home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gcc_260.zip / gcc_260 / cp / decl2.c < prev    next >
C/C++ Source or Header  |  1994-07-09  |  93KB  |  3,103 lines

  1. /* Process declarations and variables for C compiler.
  2.    Copyright (C) 1988, 1992, 1993 Free Software Foundation, Inc.
  3.    Hacked by Michael Tiemann (tiemann@cygnus.com)
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. /* Process declarations and symbol lookup for C front end.
  23.    Also constructs types; the standard scalar types at initialization,
  24.    and structure, union, array and enum types when they are declared.  */
  25.  
  26. /* ??? not all decl nodes are given the most useful possible
  27.    line numbers.  For example, the CONST_DECLs for enum values.  */
  28.  
  29. #include "config.h"
  30. #include <stdio.h>
  31. #include "tree.h"
  32. #include "rtl.h"
  33. #include "flags.h"
  34. #include "cp-tree.h"
  35. #include "decl.h"
  36. #include "lex.h"
  37.  
  38. extern tree grokdeclarator ();
  39. extern tree get_file_function_name ();
  40. extern tree cleanups_this_call;
  41. static void grok_function_init ();
  42.  
  43. /* A list of virtual function tables we must make sure to write out.  */
  44. tree pending_vtables;
  45.  
  46. /* A list of static class variables.  This is needed, because a
  47.    static class variable can be declared inside the class without
  48.    an initializer, and then initialized, staticly, outside the class.  */
  49. tree pending_statics;
  50.  
  51. /* A list of functions which were declared inline, but which we
  52.    may need to emit outline anyway. */
  53. static tree saved_inlines;
  54.  
  55. /* Used to help generate temporary names which are unique within
  56.    a function.  Reset to 0 by start_function.  */
  57.  
  58. static int temp_name_counter;
  59.  
  60. /* Same, but not reset.  Local temp variables and global temp variables
  61.    can have the same name.  */
  62. static int global_temp_name_counter;
  63.  
  64. /* Flag used when debugging spew.c */
  65.  
  66. extern int spew_debug;
  67.  
  68. /* C (and C++) language-specific option variables.  */
  69.  
  70. /* Nonzero means allow type mismatches in conditional expressions;
  71.    just make their values `void'.   */
  72.  
  73. int flag_cond_mismatch;
  74.  
  75. /* Nonzero means give `double' the same size as `float'.  */
  76.  
  77. int flag_short_double;
  78.  
  79. /* Nonzero means don't recognize the keyword `asm'.  */
  80.  
  81. int flag_no_asm;
  82.  
  83. /* Nonzero means don't recognize the non-ANSI builtin functions.  */
  84.  
  85. int flag_no_builtin;
  86.  
  87. /* Nonzero means do some things the same way PCC does.  */
  88.  
  89. int flag_traditional;
  90.  
  91. /* Nonzero means to treat bitfields as unsigned unless they say `signed'.  */
  92.  
  93. int flag_signed_bitfields = 1;
  94.  
  95. /* Nonzero means handle `#ident' directives.  0 means ignore them.  */
  96.  
  97. int flag_no_ident = 0;
  98.  
  99. /* Nonzero means disable GNU extensions.  */
  100.  
  101. int flag_ansi = 0;
  102.  
  103. /* Nonzero means do emit exported implementations of functions even if
  104.    they can be inlined.  */
  105.  
  106. int flag_implement_inlines = 1;
  107.  
  108. /* Nonzero means do emit exported implementations of templates, instead of
  109.    multiple static copies in each file that needs a definition. */
  110.  
  111. int flag_external_templates = 0;
  112.  
  113. /* Nonzero means that the decision to emit or not emit the implementation of a
  114.    template depends on where the template is instantiated, rather than where
  115.    it is defined.  */
  116.  
  117. int flag_alt_external_templates = 0;
  118.  
  119. /* Nonzero means that implicit instantiations will be emitted if needed.  */
  120.  
  121. int flag_implicit_templates = 1;
  122.  
  123. /* Nonzero means warn about implicit declarations.  */
  124.  
  125. int warn_implicit = 1;
  126.  
  127. /* Nonzero means warn when all ctors or dtors are private, and the class
  128.    has no friends.  */
  129.  
  130. int warn_ctor_dtor_privacy = 1;
  131.  
  132. /* True if we want to implement vtbvales using "thunks".
  133.    The default is off now, but will be on later.
  134.  
  135.    Also causes output of vtables to be controlled by whether
  136.    we seen the class's first non-inline virtual function. */
  137. int flag_vtable_thunks = 0;
  138.  
  139. /* Nonzero means give string constants the type `const char *'
  140.    to get extra warnings from them.  These warnings will be too numerous
  141.    to be useful, except in thoroughly ANSIfied programs.  */
  142.  
  143. int warn_write_strings;
  144.  
  145. /* Nonzero means warn about pointer casts that can drop a type qualifier
  146.    from the pointer target type.  */
  147.  
  148. int warn_cast_qual;
  149.  
  150. /* Nonzero means warn that dbx info for template class methods isn't fully
  151.    supported yet.  */
  152.  
  153. int warn_template_debugging;
  154.  
  155. /* Warn about traditional constructs whose meanings changed in ANSI C.  */
  156.  
  157. int warn_traditional;
  158.  
  159. /* Nonzero means warn about sizeof(function) or addition/subtraction
  160.    of function pointers.  */
  161.  
  162. int warn_pointer_arith;
  163.  
  164. /* Nonzero means warn for non-prototype function decls
  165.    or non-prototyped defs without previous prototype.  */
  166.  
  167. int warn_strict_prototypes;
  168.  
  169. /* Nonzero means warn for any function def without prototype decl.  */
  170.  
  171. int warn_missing_prototypes;
  172.  
  173. /* Nonzero means warn about multiple (redundant) decls for the same single
  174.    variable or function.  */
  175.  
  176. int warn_redundant_decls;
  177.  
  178. /* Warn if initializer is not completely bracketed.  */
  179.  
  180. int warn_missing_braces;
  181.  
  182. /* Warn about *printf or *scanf format/argument anomalies. */
  183.  
  184. int warn_format;
  185.  
  186. /* Warn about a subscript that has type char.  */
  187.  
  188. int warn_char_subscripts;
  189.  
  190. /* Warn if a type conversion is done that might have confusing results.  */
  191.  
  192. int warn_conversion;
  193.  
  194. /* Warn if adding () is suggested.  */
  195.  
  196. int warn_parentheses = 1;
  197.  
  198. /* Non-zero means warn in function declared in derived class has the
  199.    same name as a virtual in the base class, but fails to match the
  200.    type signature of any virtual function in the base class.  */
  201. int warn_overloaded_virtual;
  202.  
  203. /* Non-zero means warn when declaring a class that has a non virtual
  204.    destructor, when it really ought to have a virtual one. */
  205. int warn_nonvdtor;
  206.  
  207. /* Non-zero means warn when a function is declared extern and later inline.  */
  208. int warn_extern_inline;
  209.  
  210. /* Nonzero means `$' can be in an identifier.
  211.    See cccp.c for reasons why this breaks some obscure ANSI C programs.  */
  212.  
  213. #ifndef DOLLARS_IN_IDENTIFIERS
  214. #define DOLLARS_IN_IDENTIFIERS 1
  215. #endif
  216. int dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
  217.  
  218. /* Nonzero for -fno-strict-prototype switch: do not consider empty
  219.    argument prototype to mean function takes no arguments.  */
  220.  
  221. int strict_prototype = 1;
  222. int strict_prototypes_lang_c, strict_prototypes_lang_cplusplus = 1;
  223.  
  224. /* Nonzero means that labels can be used as first-class objects */
  225.  
  226. int flag_labels_ok;
  227.  
  228. /* Non-zero means to collect statistics which might be expensive
  229.    and to print them when we are done.  */
  230. int flag_detailed_statistics;
  231.  
  232. /* C++ specific flags.  */   
  233. /* Nonzero for -fall-virtual: make every member function (except
  234.    constructors) lay down in the virtual function table.  Calls
  235.    can then either go through the virtual function table or not,
  236.    depending.  */
  237.  
  238. int flag_all_virtual;
  239.  
  240. /* Zero means that `this' is a *const.  This gives nice behavior in the
  241.    2.0 world.  1 gives 1.2-compatible behavior.  2 gives Spring behavior.
  242.    -2 means we're constructing an object and it has fixed type.  */
  243.  
  244. int flag_this_is_variable;
  245.  
  246. /* Nonzero means memoize our member lookups.  */
  247.  
  248. int flag_memoize_lookups; int flag_save_memoized_contexts;
  249.  
  250. /* 3 means write out only virtuals function tables `defined'
  251.    in this implementation file.
  252.    2 means write out only specific virtual function tables
  253.    and give them (C) public access.
  254.    1 means write out virtual function tables and give them
  255.    (C) public access.
  256.    0 means write out virtual function tables and give them
  257.    (C) static access (default).
  258.    -1 means declare virtual function tables extern.  */
  259.  
  260. int write_virtuals;
  261.  
  262. /* Nonzero means we should attempt to elide constructors when possible.  */
  263.  
  264. int flag_elide_constructors;
  265.  
  266. /* Nonzero means recognize and handle exception handling constructs.
  267.    Use ansi syntax and semantics.  WORK IN PROGRESS!  */
  268.  
  269. int flag_handle_exceptions;
  270.  
  271. /* Nonzero means recognize and handle signature language constructs.  */
  272.  
  273. int flag_handle_signatures;
  274.  
  275. /* Nonzero means that member functions defined in class scope are
  276.    inline by default.  */
  277.  
  278. int flag_default_inline = 1;
  279.  
  280. /* Controls whether enums and ints freely convert.
  281.    1 means with complete freedom.
  282.    0 means enums can convert to ints, but not vice-versa.  */
  283. int flag_int_enum_equivalence;
  284.  
  285. /* Controls whether compiler is operating under LUCID's Cadillac
  286.    system.  1 means yes, 0 means no.  */
  287. int flag_cadillac;
  288.  
  289. /* Controls whether compiler generates code to build objects
  290.    that can be collected when they become garbage.  */
  291. int flag_gc;
  292.  
  293. /* Controls whether compiler generates 'dossiers' that give
  294.    run-time type information.  */
  295. int flag_dossier;
  296.  
  297. /* Nonzero if we wish to output cross-referencing information
  298.    for the GNU class browser.  */
  299. extern int flag_gnu_xref;
  300.  
  301. /* Nonzero if compiler can make `reasonable' assumptions about
  302.    references and objects.  For example, the compiler must be
  303.    conservative about the following and not assume that `a' is nonnull:
  304.  
  305.    obj &a = g ();
  306.    a.f (2);
  307.  
  308.    In general, it is `reasonable' to assume that for many programs,
  309.    and better code can be generated in that case.  */
  310.  
  311. int flag_assume_nonnull_objects;
  312.  
  313. /* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
  314.    objects. */
  315. int flag_huge_objects;
  316.  
  317. /* Nonzero if we want to conserve space in the .o files.  We do this
  318.    by putting uninitialized data and runtime initialized data into
  319.    .common instead of .data at the expense of not flaging multiple
  320.    definitions.  */
  321. int flag_conserve_space;
  322.  
  323. /* Table of language-dependent -f options.
  324.    STRING is the option name.  VARIABLE is the address of the variable.
  325.    ON_VALUE is the value to store in VARIABLE
  326.     if `-fSTRING' is seen as an option.
  327.    (If `-fno-STRING' is seen as an option, the opposite value is stored.)  */
  328.  
  329. static struct { char *string; int *variable; int on_value;} lang_f_options[] =
  330. {
  331.   {"signed-char", &flag_signed_char, 1},
  332.   {"unsigned-char", &flag_signed_char, 0},
  333.   {"signed-bitfields", &flag_signed_bitfields, 1},
  334.   {"unsigned-bitfields", &flag_signed_bitfields, 0},
  335.   {"short-enums", &flag_short_enums, 1},
  336.   {"short-double", &flag_short_double, 1},
  337.   {"cond-mismatch", &flag_cond_mismatch, 1},
  338.   {"asm", &flag_no_asm, 0},
  339.   {"builtin", &flag_no_builtin, 0},
  340.   {"ident", &flag_no_ident, 0},
  341.   {"labels-ok", &flag_labels_ok, 1},
  342.   {"stats", &flag_detailed_statistics, 1},
  343.   {"this-is-variable", &flag_this_is_variable, 1},
  344.   {"strict-prototype", &strict_prototypes_lang_cplusplus, 1},
  345.   {"all-virtual", &flag_all_virtual, 1},
  346.   {"memoize-lookups", &flag_memoize_lookups, 1},
  347.   {"elide-constructors", &flag_elide_constructors, 1},
  348.   {"handle-exceptions", &flag_handle_exceptions, 1},
  349.   {"handle-signatures", &flag_handle_signatures, 1},
  350.   {"default-inline", &flag_default_inline, 1},
  351.   {"dollars-in-identifiers", &dollars_in_ident, 1},
  352.   {"enum-int-equiv", &flag_int_enum_equivalence, 1},
  353.   {"gc", &flag_gc, 1},
  354.   {"dossier", &flag_dossier, 1},
  355.   {"xref", &flag_gnu_xref, 1},
  356.   {"nonnull-objects", &flag_assume_nonnull_objects, 1},
  357.   {"implement-inlines", &flag_implement_inlines, 1},
  358.   {"external-templates", &flag_external_templates, 1},
  359.   {"implicit-templates", &flag_implicit_templates, 1},
  360.   {"huge-objects", &flag_huge_objects, 1},
  361.   {"conserve-space", &flag_conserve_space, 1},
  362.   {"vtable-thunks", &flag_vtable_thunks, 1},
  363.   {"short-temps", &flag_short_temps, 1},
  364. };
  365.  
  366. /* Decode the string P as a language-specific option.
  367.    Return 1 if it is recognized (and handle it);
  368.    return 0 if not recognized.  */
  369.  
  370. int   
  371. lang_decode_option (p)
  372.      char *p;
  373. {
  374.   if (!strcmp (p, "-ftraditional") || !strcmp (p, "-traditional"))
  375.     flag_traditional = 1, dollars_in_ident = 1, flag_writable_strings = 1,
  376.     flag_this_is_variable = 1;
  377.   /* The +e options are for cfront compatibility.  They come in as
  378.      `-+eN', to kludge around gcc.c's argument handling.  */
  379.   else if (p[0] == '-' && p[1] == '+' && p[2] == 'e')
  380.     {
  381.       int old_write_virtuals = write_virtuals;
  382.       if (p[3] == '1')
  383.     write_virtuals = 1;
  384.       else if (p[3] == '0')
  385.     write_virtuals = -1;
  386.       else if (p[3] == '2')
  387.     write_virtuals = 2;
  388.       else error ("invalid +e option");
  389.       if (old_write_virtuals != 0
  390.       && write_virtuals != old_write_virtuals)
  391.     error ("conflicting +e options given");
  392.     }
  393.   else if (p[0] == '-' && p[1] == 'f')
  394.     {
  395.       /* Some kind of -f option.
  396.      P's value is the option sans `-f'.
  397.      Search for it in the table of options.  */
  398.       int found = 0, j;
  399.  
  400.       p += 2;
  401.       /* Try special -f options.  */
  402.  
  403.       if (!strcmp (p, "save-memoized"))
  404.     {
  405.       flag_memoize_lookups = 1;
  406.       flag_save_memoized_contexts = 1;
  407.       found = 1;
  408.     }
  409.       if (!strcmp (p, "no-save-memoized"))
  410.     {
  411.       flag_memoize_lookups = 0;
  412.       flag_save_memoized_contexts = 0;
  413.       found = 1;
  414.     }
  415.       else if (! strncmp (p, "cadillac", 8))
  416.     {
  417.       flag_cadillac = atoi (p+9);
  418.       found = 1;
  419.     }
  420.       else if (! strncmp (p, "no-cadillac", 11))
  421.     {
  422.       flag_cadillac = 0;
  423.       found = 1;
  424.     }
  425.       else if (! strcmp (p, "gc"))
  426.     {
  427.       flag_gc = 1;
  428.       /* This must come along for the ride.  */
  429.       flag_dossier = 1;
  430.       found = 1;
  431.     }
  432.       else if (! strcmp (p, "no-gc"))
  433.     {
  434.       flag_gc = 0;
  435.       /* This must come along for the ride.  */
  436.       flag_dossier = 0;
  437.       found = 1;
  438.     }
  439.       else if (! strcmp (p, "alt-external-templates"))
  440.     {
  441.       flag_external_templates = 1;
  442.       flag_alt_external_templates = 1;
  443.       found = 1;
  444.     }
  445.       else if (! strcmp (p, "no-alt-external-templates"))
  446.     {
  447.       flag_alt_external_templates = 0;
  448.       found = 1;
  449.     }
  450.       else for (j = 0;
  451.         !found && j < sizeof (lang_f_options) / sizeof (lang_f_options[0]);
  452.         j++)
  453.     {
  454.       if (!strcmp (p, lang_f_options[j].string))
  455.         {
  456.           *lang_f_options[j].variable = lang_f_options[j].on_value;
  457.           /* A goto here would be cleaner,
  458.          but breaks the vax pcc.  */
  459.           found = 1;
  460.         }
  461.       if (p[0] == 'n' && p[1] == 'o' && p[2] == '-'
  462.           && ! strcmp (p+3, lang_f_options[j].string))
  463.         {
  464.           *lang_f_options[j].variable = ! lang_f_options[j].on_value;
  465.           found = 1;
  466.         }
  467.     }
  468.       return found;
  469.     }
  470.   else if (p[0] == '-' && p[1] == 'W')
  471.     {
  472.       int setting = 1;
  473.  
  474.       /* The -W options control the warning behavior of the compiler.  */
  475.       p += 2;
  476.  
  477.       if (p[0] == 'n' && p[1] == 'o' && p[2] == '-')
  478.     setting = 0, p += 3;
  479.  
  480.       if (!strcmp (p, "implicit"))
  481.     warn_implicit = setting;
  482.       else if (!strcmp (p, "return-type"))
  483.     warn_return_type = setting;
  484.       else if (!strcmp (p, "ctor-dtor-privacy"))
  485.     warn_ctor_dtor_privacy = setting;
  486.       else if (!strcmp (p, "write-strings"))
  487.     warn_write_strings = setting;
  488.       else if (!strcmp (p, "cast-qual"))
  489.     warn_cast_qual = setting;
  490.       else if (!strcmp (p, "traditional"))
  491.     warn_traditional = setting;
  492.       else if (!strcmp (p, "char-subscripts"))
  493.     warn_char_subscripts = setting;
  494.       else if (!strcmp (p, "pointer-arith"))
  495.     warn_pointer_arith = setting;
  496.       else if (!strcmp (p, "strict-prototypes"))
  497.     warn_strict_prototypes = setting;
  498.       else if (!strcmp (p, "missing-prototypes"))
  499.     warn_missing_prototypes = setting;
  500.       else if (!strcmp (p, "redundant-decls"))
  501.     warn_redundant_decls = setting;
  502.       else if (!strcmp (p, "missing-braces"))
  503.     warn_missing_braces = setting;
  504.       else if (!strcmp (p, "format"))
  505.     warn_format = setting;
  506.       else if (!strcmp (p, "conversion"))
  507.     warn_conversion = setting;
  508.       else if (!strcmp (p, "parentheses"))
  509.     warn_parentheses = setting;
  510.       else if (!strcmp (p, "non-virtual-dtor"))
  511.     warn_nonvdtor = setting;
  512.       else if (!strcmp (p, "extern-inline"))
  513.     warn_extern_inline = setting;
  514.       else if (!strcmp (p, "comment"))
  515.     ;            /* cpp handles this one.  */
  516.       else if (!strcmp (p, "comments"))
  517.     ;            /* cpp handles this one.  */
  518.       else if (!strcmp (p, "trigraphs"))
  519.     ;            /* cpp handles this one.  */
  520.       else if (!strcmp (p, "import"))
  521.     ;            /* cpp handles this one.  */
  522.       else if (!strcmp (p, "all"))
  523.     {
  524.       extra_warnings = setting;
  525.       warn_return_type = setting;
  526.       warn_unused = setting;
  527.       warn_implicit = setting;
  528.       warn_ctor_dtor_privacy = setting;
  529.       warn_switch = setting;
  530.       warn_format = setting;
  531.       warn_missing_braces = setting;
  532.       warn_extern_inline = setting;
  533.       warn_nonvdtor = setting;
  534.       /* We save the value of warn_uninitialized, since if they put
  535.          -Wuninitialized on the command line, we need to generate a
  536.          warning about not using it without also specifying -O.  */
  537.       if (warn_uninitialized != 1)
  538.         warn_uninitialized = (setting ? 2 : 0);
  539.       warn_template_debugging = setting;
  540.     }
  541.  
  542.       else if (!strcmp (p, "overloaded-virtual"))
  543.     warn_overloaded_virtual = setting;
  544.       else return 0;
  545.     }
  546.   else if (!strcmp (p, "-ansi"))
  547.     flag_no_asm = 1, dollars_in_ident = 0, flag_ansi = 1;
  548. #ifdef SPEW_DEBUG
  549.   /* Undocumented, only ever used when you're invoking cc1plus by hand, since
  550.      it's probably safe to assume no sane person would ever want to use this
  551.      under normal circumstances.  */
  552.   else if (!strcmp (p, "-spew-debug"))
  553.     spew_debug = 1;
  554. #endif
  555.   else
  556.     return 0;
  557.  
  558.   return 1;
  559. }
  560.  
  561. /* Incorporate `const' and `volatile' qualifiers for member functions.
  562.    FUNCTION is a TYPE_DECL or a FUNCTION_DECL.
  563.    QUALS is a list of qualifiers.  */
  564. tree
  565. grok_method_quals (ctype, function, quals)
  566.      tree ctype, function, quals;
  567. {
  568.   tree fntype = TREE_TYPE (function);
  569.   tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
  570.  
  571.   do
  572.     {
  573.       extern tree ridpointers[];
  574.  
  575.       if (TREE_VALUE (quals) == ridpointers[(int)RID_CONST])
  576.     {
  577.       if (TYPE_READONLY (ctype))
  578.         error ("duplicate `%s' %s",
  579.            IDENTIFIER_POINTER (TREE_VALUE (quals)),
  580.            (TREE_CODE (function) == FUNCTION_DECL
  581.             ? "for member function" : "in type declaration"));
  582.       ctype = build_type_variant (ctype, 1, TYPE_VOLATILE (ctype));
  583.       build_pointer_type (ctype);
  584.     }
  585.       else if (TREE_VALUE (quals) == ridpointers[(int)RID_VOLATILE])
  586.     {
  587.       if (TYPE_VOLATILE (ctype))
  588.         error ("duplicate `%s' %s",
  589.            IDENTIFIER_POINTER (TREE_VALUE (quals)),
  590.            (TREE_CODE (function) == FUNCTION_DECL
  591.             ? "for member function" : "in type declaration"));
  592.       ctype = build_type_variant (ctype, TYPE_READONLY (ctype), 1);
  593.       build_pointer_type (ctype);
  594.     }
  595.       else
  596.     my_friendly_abort (20);
  597.       quals = TREE_CHAIN (quals);
  598.     }
  599.   while (quals);
  600.   fntype = build_cplus_method_type (ctype, TREE_TYPE (fntype),
  601.                     (TREE_CODE (fntype) == METHOD_TYPE
  602.                      ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
  603.                      : TYPE_ARG_TYPES (fntype)));
  604.   if (raises)
  605.     fntype = build_exception_variant (ctype, fntype, raises);
  606.  
  607.   TREE_TYPE (function) = fntype;
  608.   return ctype;
  609. }
  610.  
  611. #if 0                /* Not used. */
  612. /* This routine replaces cryptic DECL_NAMEs with readable DECL_NAMEs.
  613.    It leaves DECL_ASSEMBLER_NAMEs with the correct value.  */
  614. /* This does not yet work with user defined conversion operators
  615.    It should.  */
  616. static void
  617. substitute_nice_name (decl)
  618.      tree decl;
  619. {
  620.   if (DECL_NAME (decl) && TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE)
  621.     {
  622.       char *n = decl_as_string (DECL_NAME (decl), 1);
  623.       if (n[strlen (n) - 1] == ' ')
  624.     n[strlen (n) - 1] = 0;
  625.       DECL_NAME (decl) = get_identifier (n);
  626.     }
  627. }
  628. #endif
  629.  
  630. /* Warn when -fexternal-templates is used and #pragma
  631.    interface/implementation is not used all the times it should be,
  632.    inform the user.  */
  633. void
  634. warn_if_unknown_interface ()
  635. {
  636.   static int already_warned = 0;
  637.   if (++already_warned == 1)
  638.     warning ("templates that are built with -fexternal-templates should be in files that have #pragma interface/implementation");
  639. }
  640.  
  641. /* A subroutine of the parser, to handle a component list.  */
  642. tree
  643. grok_x_components (specs, components)
  644.      tree specs, components;
  645. {
  646.   register tree t, x, tcode;
  647.  
  648.   /* We just got some friends.  They have been recorded elsewhere.  */
  649.   if (components == void_type_node)
  650.     return NULL_TREE;
  651.  
  652.   if (components == NULL_TREE)
  653.     {
  654.       t = groktypename (build_decl_list (specs, NULL_TREE));
  655.  
  656.       if (t == NULL_TREE)
  657.     {
  658.       error ("error in component specification");
  659.       return NULL_TREE;
  660.     }
  661.  
  662.       switch (TREE_CODE (t))
  663.     {
  664.     case VAR_DECL:
  665.       /* Static anonymous unions come out as VAR_DECLs.  */
  666.       if (TREE_CODE (TREE_TYPE (t)) == UNION_TYPE
  667.           && ANON_AGGRNAME_P (TYPE_IDENTIFIER (TREE_TYPE (t))))
  668.         return t;
  669.  
  670.       /* We return SPECS here, because in the parser it was ending
  671.          up with not doing anything to $$, which is what SPECS
  672.          represents.  */
  673.       return specs;
  674.       break;
  675.  
  676.     case RECORD_TYPE:
  677.       /* This code may be needed for UNION_TYPEs as
  678.          well.  */
  679.       tcode = record_type_node;
  680.       if (CLASSTYPE_DECLARED_CLASS(t))
  681.         tcode = class_type_node;
  682.       else if (IS_SIGNATURE(t))
  683.         tcode = signature_type_node;
  684.       else if (CLASSTYPE_DECLARED_EXCEPTION(t))
  685.         tcode = exception_type_node;
  686.       
  687.       t = xref_defn_tag(tcode, TYPE_IDENTIFIER(t), NULL_TREE);
  688.       if (TYPE_CONTEXT(t))
  689.         CLASSTYPE_NO_GLOBALIZE(t) = 1;
  690.       if (TYPE_LANG_SPECIFIC (t)
  691.           && CLASSTYPE_DECLARED_EXCEPTION (t))
  692.         shadow_tag (specs);
  693.       return NULL_TREE;
  694.       break;
  695.  
  696.     case UNION_TYPE:
  697.     case ENUMERAL_TYPE:
  698.       if (TREE_CODE(t) == UNION_TYPE)
  699.         tcode = union_type_node;
  700.       else
  701.         tcode = enum_type_node;
  702.  
  703.       t = xref_defn_tag(tcode, TYPE_IDENTIFIER(t), NULL_TREE);
  704.       if (TREE_CODE(t) == UNION_TYPE && TYPE_CONTEXT(t))
  705.         CLASSTYPE_NO_GLOBALIZE(t) = 1;
  706.       if (TREE_CODE (t) == UNION_TYPE
  707.           && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
  708.         {
  709.           struct pending_inline **p;
  710.           x = build_lang_field_decl (FIELD_DECL, NULL_TREE, t);
  711.  
  712.           /* Wipe out memory of synthesized methods */
  713.           TYPE_HAS_CONSTRUCTOR (t) = 0;
  714.           TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
  715.           TYPE_HAS_INIT_REF (t) = 0;
  716.           TYPE_HAS_CONST_INIT_REF (t) = 0;
  717.           TYPE_HAS_ASSIGN_REF (t) = 0;
  718.           TYPE_HAS_ASSIGNMENT (t) = 0;
  719.           TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
  720.  
  721.           p = &pending_inlines;
  722.           for (; *p; *p = (*p)->next)
  723.         if (DECL_CONTEXT ((*p)->fndecl) != t)
  724.           break;
  725.         }
  726.       else if (TREE_CODE (t) == ENUMERAL_TYPE)
  727.         x = grok_enum_decls (t, NULL_TREE);
  728.       else
  729.         x = NULL_TREE;
  730.       return x;
  731.       break;
  732.  
  733.     default:
  734.       if (t != void_type_node)
  735.         error ("empty component declaration");
  736.       return NULL_TREE;
  737.     }
  738.     }
  739.   else
  740.     {
  741.       t = TREE_TYPE (components);
  742.       if (TREE_CODE (t) == ENUMERAL_TYPE && TREE_NONLOCAL_FLAG (t))
  743.     return grok_enum_decls (t, components);
  744.       else
  745.     return components;
  746.     }
  747. }
  748.  
  749. /* Classes overload their constituent function names automatically.
  750.    When a function name is declared in a record structure,
  751.    its name is changed to it overloaded name.  Since names for
  752.    constructors and destructors can conflict, we place a leading
  753.    '$' for destructors.
  754.  
  755.    CNAME is the name of the class we are grokking for.
  756.  
  757.    FUNCTION is a FUNCTION_DECL.  It was created by `grokdeclarator'.
  758.  
  759.    FLAGS contains bits saying what's special about today's
  760.    arguments.  1 == DESTRUCTOR.  2 == OPERATOR.
  761.  
  762.    If FUNCTION is a destructor, then we must add the `auto-delete' field
  763.    as a second parameter.  There is some hair associated with the fact
  764.    that we must "declare" this variable in the manner consistent with the
  765.    way the rest of the arguments were declared.
  766.  
  767.    QUALS are the qualifiers for the this pointer.  */
  768.  
  769. void
  770. grokclassfn (ctype, cname, function, flags, quals)
  771.      tree ctype, cname, function;
  772.      enum overload_flags flags;
  773.      tree quals;
  774. {
  775.   tree fn_name = DECL_NAME (function);
  776.   tree arg_types;
  777.   tree parm;
  778.   tree qualtype;
  779.  
  780.   if (fn_name == NULL_TREE)
  781.     {
  782.       error ("name missing for member function");
  783.       fn_name = get_identifier ("<anonymous>");
  784.       DECL_NAME (function) = fn_name;
  785.     }
  786.  
  787.   if (quals)
  788.     qualtype = grok_method_quals (ctype, function, quals);
  789.   else
  790.     qualtype = ctype;
  791.  
  792.   arg_types = TYPE_ARG_TYPES (TREE_TYPE (function));
  793.   if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
  794.     {
  795.       /* Must add the class instance variable up front.  */
  796.       /* Right now we just make this a pointer.  But later
  797.      we may wish to make it special.  */
  798.       tree type = TREE_VALUE (arg_types);
  799.  
  800.       if ((flag_this_is_variable > 0)
  801.       && (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function)))
  802.     type = TYPE_MAIN_VARIANT (type);
  803.  
  804.       if (DECL_CONSTRUCTOR_P (function))
  805.     {
  806.       if (TYPE_USES_VIRTUAL_BASECLASSES (ctype))
  807.         {
  808.           DECL_CONSTRUCTOR_FOR_VBASE_P (function) = 1;
  809.           /* In this case we need "in-charge" flag saying whether
  810.          this constructor is responsible for initialization
  811.          of virtual baseclasses or not.  */
  812.           parm = build_decl (PARM_DECL, in_charge_identifier, integer_type_node);
  813.           /* Mark the artificial `__in_chrg' parameter as "artificial".  */
  814.           SET_DECL_ARTIFICIAL (parm);
  815.           DECL_ARG_TYPE (parm) = integer_type_node;
  816.           DECL_REGISTER (parm) = 1;
  817.           TREE_CHAIN (parm) = last_function_parms;
  818.           last_function_parms = parm;
  819.         }
  820.     }
  821.  
  822.       parm = build_decl (PARM_DECL, this_identifier, type);
  823.       /* Mark the artificial `this' parameter as "artificial".  */
  824.       SET_DECL_ARTIFICIAL (parm);
  825.       DECL_ARG_TYPE (parm) = type;
  826.       /* We can make this a register, so long as we don't
  827.      accidentally complain if someone tries to take its address.  */
  828.       DECL_REGISTER (parm) = 1;
  829.       if (TYPE_READONLY (type))
  830.     TREE_READONLY (parm) = 1;
  831.       TREE_CHAIN (parm) = last_function_parms;
  832.       last_function_parms = parm;
  833.     }
  834.  
  835.   if (flags == DTOR_FLAG)
  836.     {
  837.       char *buf, *dbuf;
  838.       tree const_integer_type = build_type_variant (integer_type_node, 1, 0);
  839.       int len = sizeof (DESTRUCTOR_DECL_PREFIX)-1;
  840.  
  841.       arg_types = hash_tree_chain (const_integer_type, void_list_node);
  842.       TREE_SIDE_EFFECTS (arg_types) = 1;
  843.       /* Build the overload name.  It will look like `7Example'.  */
  844.       if (IDENTIFIER_TYPE_VALUE (cname))
  845.     dbuf = build_overload_name (IDENTIFIER_TYPE_VALUE (cname), 1, 1);
  846.       else if (IDENTIFIER_LOCAL_VALUE (cname))
  847.     dbuf = build_overload_name (TREE_TYPE (IDENTIFIER_LOCAL_VALUE (cname)), 1, 1);
  848.       else
  849.       /* Using ctype fixes the `X::Y::~Y()' crash.  The cname has no type when
  850.      it's defined out of the class definition, since poplevel_class wipes
  851.      it out.  This used to be internal error 346.  */
  852.     dbuf = build_overload_name (ctype, 1, 1);
  853.       buf = (char *) alloca (strlen (dbuf) + sizeof (DESTRUCTOR_DECL_PREFIX));
  854.       bcopy (DESTRUCTOR_DECL_PREFIX, buf, len);
  855.       buf[len] = '\0';
  856.       strcat (buf, dbuf);
  857.       DECL_ASSEMBLER_NAME (function) = get_identifier (buf);
  858.       parm = build_decl (PARM_DECL, in_charge_identifier, const_integer_type);
  859.       /* Mark the artificial `__in_chrg' parameter as "artificial".  */
  860.       SET_DECL_ARTIFICIAL (parm);
  861.       TREE_USED (parm) = 1;
  862. #if 0
  863.       /* We don't need to mark the __in_chrg parameter itself as `const'
  864.       since its type is already `const int'.  In fact we MUST NOT mark
  865.       it as `const' cuz that will screw up the debug info (causing it
  866.       to say that the type of __in_chrg is `const const int').  */
  867.       TREE_READONLY (parm) = 1;
  868. #endif
  869.       DECL_ARG_TYPE (parm) = const_integer_type;
  870.       /* This is the same chain as DECL_ARGUMENTS (...).  */
  871.       TREE_CHAIN (last_function_parms) = parm;
  872.  
  873.       TREE_TYPE (function) = build_cplus_method_type (qualtype, void_type_node,
  874.                               arg_types);
  875.       TYPE_HAS_DESTRUCTOR (ctype) = 1;
  876.     }
  877.   else
  878.     {
  879.       tree these_arg_types;
  880.  
  881.       if (DECL_CONSTRUCTOR_FOR_VBASE_P (function))
  882.     {
  883.       arg_types = hash_tree_chain (integer_type_node,
  884.                        TREE_CHAIN (arg_types));
  885.       TREE_TYPE (function)
  886.         = build_cplus_method_type (qualtype,
  887.                        TREE_TYPE (TREE_TYPE (function)),
  888.                        arg_types);
  889.       arg_types = TYPE_ARG_TYPES (TREE_TYPE (function));
  890.     }
  891.  
  892.       these_arg_types = arg_types;
  893.  
  894.       if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
  895.     /* Only true for static member functions.  */
  896.     these_arg_types = hash_tree_chain (TYPE_POINTER_TO (qualtype),
  897.                        arg_types);
  898.  
  899.       DECL_ASSEMBLER_NAME (function)
  900.     = build_decl_overload (fn_name, these_arg_types,
  901.                    1 + DECL_CONSTRUCTOR_P (function));
  902.  
  903. #if 0
  904.       /* This code is going into the compiler, but currently, it makes
  905.      libg++/src/Interger.cc not compile.  The problem is that the nice name
  906.      winds up going into the symbol table, and conversion operations look
  907.      for the manged name.  */
  908.       substitute_nice_name (function);
  909. #endif
  910.     }
  911.  
  912.   DECL_ARGUMENTS (function) = last_function_parms;
  913.   /* First approximations.  */
  914.   DECL_CONTEXT (function) = ctype;
  915.   DECL_CLASS_CONTEXT (function) = ctype;
  916. }
  917.  
  918. /* Work on the expr used by alignof (this is only called by the parser).  */
  919. tree
  920. grok_alignof (expr)
  921.      tree expr;
  922. {
  923.   tree best, t;
  924.   int bestalign;
  925.  
  926.   if (TREE_CODE (expr) == COMPONENT_REF
  927.       && DECL_BIT_FIELD (TREE_OPERAND (expr, 1)))
  928.     error ("`__alignof__' applied to a bit-field");
  929.  
  930.   if (TREE_CODE (expr) == INDIRECT_REF)
  931.     {
  932.       best = t = TREE_OPERAND (expr, 0);
  933.       bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
  934.  
  935.       while (TREE_CODE (t) == NOP_EXPR
  936.          && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
  937.     {
  938.       int thisalign;
  939.       t = TREE_OPERAND (t, 0);
  940.       thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
  941.       if (thisalign > bestalign)
  942.         best = t, bestalign = thisalign;
  943.     }
  944.       return c_alignof (TREE_TYPE (TREE_TYPE (best)));
  945.     }
  946.   else
  947.     {
  948.       /* ANSI says arrays and fns are converted inside comma.
  949.      But we can't convert them in build_compound_expr
  950.      because that would break commas in lvalues.
  951.      So do the conversion here if operand was a comma.  */
  952.       if (TREE_CODE (expr) == COMPOUND_EXPR
  953.       && (TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
  954.           || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE))
  955.     expr = default_conversion (expr);
  956.       return c_alignof (TREE_TYPE (expr));
  957.     }
  958. }
  959.  
  960. /* Create an ARRAY_REF, checking for the user doing things backwards
  961.    along the way.  */
  962. tree
  963. grok_array_decl (array_expr, index_exp)
  964.      tree array_expr, index_exp;
  965. {
  966.   tree type = TREE_TYPE (array_expr);
  967.  
  968.   if (type == error_mark_node || index_exp == error_mark_node)
  969.     return error_mark_node;
  970.   if (type == NULL_TREE)
  971.     {
  972.       /* Something has gone very wrong.  Assume we are mistakenly reducing
  973.      an expression instead of a declaration.  */
  974.       error ("parser may be lost: is there a '{' missing somewhere?");
  975.       return NULL_TREE;
  976.     }
  977.  
  978.   if (TREE_CODE (type) == OFFSET_TYPE
  979.       || TREE_CODE (type) == REFERENCE_TYPE)
  980.     type = TREE_TYPE (type);
  981.  
  982.   /* If they have an `operator[]', use that.  */
  983.   if (TYPE_LANG_SPECIFIC (type)
  984.       && TYPE_OVERLOADS_ARRAY_REF (type))
  985.     return build_opfncall (ARRAY_REF, LOOKUP_NORMAL,
  986.              array_expr, index_exp, NULL_TREE);
  987.  
  988.   /* Otherwise, create an ARRAY_REF for a pointer or array type.  */
  989.   if (TREE_CODE (type) == POINTER_TYPE
  990.       || TREE_CODE (type) == ARRAY_TYPE)
  991.     return build_array_ref (array_expr, index_exp);
  992.  
  993.   /* Woops, looks like they did something like `5[a]' instead of `a[5]'.
  994.      We don't emit a warning or error for this, since it's allowed
  995.      by ARM $8.2.4.  */
  996.  
  997.   type = TREE_TYPE (index_exp);
  998.  
  999.   if (TREE_CODE (type) == OFFSET_TYPE
  1000.       || TREE_CODE (type) == REFERENCE_TYPE)
  1001.     type = TREE_TYPE (type);
  1002.  
  1003.   if (TYPE_LANG_SPECIFIC (type)
  1004.       && TYPE_OVERLOADS_ARRAY_REF (type))
  1005.     error ("array expression backwards");
  1006.   else if (TREE_CODE (type) == POINTER_TYPE
  1007.        || TREE_CODE (type) == ARRAY_TYPE)
  1008.     return build_array_ref (index_exp, array_expr);
  1009.   else
  1010.     error("`[]' applied to non-pointer type");
  1011.  
  1012.   /* We gave an error, so give an error.  Huh?  */
  1013.   return error_mark_node;
  1014. }
  1015.  
  1016. /* Given the cast expression EXP, checking out its validity.   Either return
  1017.    an error_mark_node if there was an unavoidable error, return a cast to
  1018.    void for trying to delete a pointer w/ the value 0, or return the
  1019.    call to delete.  If DOING_VEC is 1, we handle things differently
  1020.    for doing an array delete.  If DOING_VEC is 2, they gave us the
  1021.    array size as an argument to delete.
  1022.    Implements ARM $5.3.4.  This is called from the parser.  */
  1023. tree
  1024. delete_sanity (exp, size, doing_vec, use_global_delete)
  1025.      tree exp, size;
  1026.      int doing_vec, use_global_delete;
  1027. {
  1028.   tree t = stabilize_reference (convert_from_reference (exp));
  1029.   tree type = TREE_TYPE (t);
  1030.   enum tree_code code = TREE_CODE (type);
  1031.   /* For a regular vector delete (aka, no size argument) we will pass
  1032.      this down as a NULL_TREE into build_vec_delete.  */
  1033.   tree maxindex = NULL_TREE;
  1034.   /* This is used for deleting arrays.  */
  1035.   tree elt_size;
  1036.  
  1037.   switch (doing_vec)
  1038.     {
  1039.     case 2:
  1040.       maxindex = build_binary_op (MINUS_EXPR, size, integer_one_node, 1);
  1041.       if (! flag_traditional)
  1042.     pedwarn ("anachronistic use of array size in vector delete");
  1043.       /* Fall through.  */
  1044.     case 1:
  1045.       elt_size = c_sizeof (type);
  1046.       break;
  1047.     default:
  1048.       if (code != POINTER_TYPE)
  1049.     {
  1050.       cp_error ("type `%#T' argument given to `delete', expected pointer",
  1051.             type);
  1052.       return error_mark_node;
  1053.     }
  1054.  
  1055.       /* Deleting a pointer with the value zero is legal and has no effect.  */
  1056.       if (integer_zerop (t))
  1057.     return build1 (NOP_EXPR, void_type_node, t);
  1058.     }
  1059.  
  1060.   /* You can't delete a pointer to constant.  */
  1061.   if (code == POINTER_TYPE && TREE_READONLY (TREE_TYPE (type)))
  1062.     {
  1063.       error ("`const *' cannot be deleted");
  1064.       return error_mark_node;
  1065.     }
  1066.  
  1067. #if 0
  1068.   /* If the type has no destructor, then we should build a regular
  1069.      delete, instead of a vector delete.  Otherwise, we would end
  1070.      up passing a bogus offset into __builtin_delete, which is
  1071.      not expecting it.  */ 
  1072.   if (doing_vec
  1073.       && TREE_CODE (type) == POINTER_TYPE
  1074.       && !TYPE_HAS_DESTRUCTOR (TREE_TYPE (type)))
  1075.     {
  1076.       doing_vec = 0;
  1077.       use_global_delete = 1;
  1078.     }
  1079. #endif
  1080.  
  1081.   if (doing_vec)
  1082.     return build_vec_delete (t, maxindex, elt_size, integer_one_node,
  1083.                  integer_two_node, use_global_delete);
  1084.   else
  1085.     return build_delete (type, t, integer_three_node,
  1086.              LOOKUP_NORMAL|LOOKUP_HAS_IN_CHARGE,
  1087.              use_global_delete);
  1088. }
  1089.  
  1090. /* Sanity check: report error if this function FUNCTION is not
  1091.    really a member of the class (CTYPE) it is supposed to belong to.
  1092.    CNAME is the same here as it is for grokclassfn above.  */
  1093.  
  1094. void
  1095. check_classfn (ctype, cname, function)
  1096.      tree ctype, cname, function;
  1097. {
  1098.   tree fn_name = DECL_NAME (function);
  1099.   tree fndecl;
  1100.   tree method_vec = CLASSTYPE_METHOD_VEC (ctype);
  1101.   tree *methods = 0;
  1102.   tree *end = 0;
  1103.  
  1104.   if (method_vec != 0)
  1105.     {
  1106.       methods = &TREE_VEC_ELT (method_vec, 0);
  1107.       end = TREE_VEC_END (method_vec);
  1108.  
  1109.       /* First suss out ctors and dtors.  */
  1110.       if (*methods && fn_name == cname)
  1111.     goto got_it;
  1112.  
  1113.       while (++methods != end)
  1114.     {
  1115.       if (fn_name == DECL_NAME (*methods))
  1116.         {
  1117.         got_it:
  1118.           fndecl = *methods;
  1119.           while (fndecl)
  1120.         {
  1121.           if (DECL_ASSEMBLER_NAME (function) == DECL_ASSEMBLER_NAME (fndecl))
  1122.             return;
  1123.           fndecl = DECL_CHAIN (fndecl);
  1124.         }
  1125.           break;        /* loser */
  1126.         }
  1127.     }
  1128.     }
  1129.  
  1130.   if (methods != end)
  1131.     cp_error ("argument list for `%D' does not match any in class `%T'",
  1132.           fn_name, ctype);
  1133.   else
  1134.     {
  1135.       methods = 0;
  1136.       cp_error ("no `%D' member function declared in class `%T'",
  1137.         fn_name, ctype);
  1138.     }
  1139.  
  1140.   /* If we did not find the method in the class, add it to
  1141.      avoid spurious errors.  */
  1142.   add_method (ctype, methods, function);
  1143. }
  1144.  
  1145. /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
  1146.    of a structure component, returning a FIELD_DECL node.
  1147.    QUALS is a list of type qualifiers for this decl (such as for declaring
  1148.    const member functions).
  1149.  
  1150.    This is done during the parsing of the struct declaration.
  1151.    The FIELD_DECL nodes are chained together and the lot of them
  1152.    are ultimately passed to `build_struct' to make the RECORD_TYPE node.
  1153.  
  1154.    C++:
  1155.  
  1156.    If class A defines that certain functions in class B are friends, then
  1157.    the way I have set things up, it is B who is interested in permission
  1158.    granted by A.  However, it is in A's context that these declarations
  1159.    are parsed.  By returning a void_type_node, class A does not attempt
  1160.    to incorporate the declarations of the friends within its structure.
  1161.  
  1162.    DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
  1163.    CHANGES TO CODE IN `start_method'.  */
  1164.  
  1165. tree
  1166. grokfield (declarator, declspecs, raises, init, asmspec_tree)
  1167.      tree declarator, declspecs, raises, init, asmspec_tree;
  1168. {
  1169.   register tree value;
  1170.   char *asmspec = 0;
  1171.  
  1172.   /* Convert () initializers to = initializers.  */
  1173.   if (init == NULL_TREE && declarator != NULL_TREE
  1174.       && TREE_CODE (declarator) == CALL_EXPR
  1175.       && TREE_OPERAND (declarator, 0)
  1176.       && (TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE
  1177.       || TREE_CODE (TREE_OPERAND (declarator, 0)) == SCOPE_REF)
  1178.       && parmlist_is_exprlist (TREE_OPERAND (declarator, 1)))
  1179.     {
  1180.       init = TREE_OPERAND (declarator, 1);
  1181.       declarator = TREE_OPERAND (declarator, 0);
  1182.     }
  1183.  
  1184.   if (init
  1185.       && TREE_CODE (init) == TREE_LIST
  1186.       && TREE_VALUE (init) == error_mark_node
  1187.       && TREE_CHAIN (init) == NULL_TREE)
  1188.     init = NULL_TREE;
  1189.  
  1190.   value = grokdeclarator (declarator, declspecs, FIELD, init != 0, raises);
  1191.   if (! value)
  1192.     return value; /* friend or constructor went bad.  */
  1193.  
  1194.   /* Pass friendly classes back.  */
  1195.   if (TREE_CODE (value) == VOID_TYPE)
  1196.     return void_type_node;
  1197.  
  1198.   if (DECL_NAME (value) != NULL_TREE
  1199.       && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
  1200.       && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
  1201.     cp_error ("member `%D' conflicts with virtual function table field name", value);
  1202.  
  1203.   /* Stash away type declarations.  */
  1204.   if (TREE_CODE (value) == TYPE_DECL)
  1205.     {
  1206.       DECL_NONLOCAL (value) = 1;
  1207.       DECL_CONTEXT (value) = current_class_type;
  1208.       DECL_CLASS_CONTEXT (value) = current_class_type;
  1209.       CLASSTYPE_LOCAL_TYPEDECLS (current_class_type) = 1;
  1210.       pushdecl_class_level (value);
  1211.       return value;
  1212.     }
  1213.  
  1214.   if (IS_SIGNATURE (current_class_type)
  1215.       && TREE_CODE (value) != FUNCTION_DECL)
  1216.     {
  1217.       error ("field declaration not allowed in signature");
  1218.       return void_type_node;
  1219.     }
  1220.  
  1221.   if (DECL_IN_AGGR_P (value))
  1222.     {
  1223.       cp_error ("`%D' is already defined in the class %T", value,
  1224.           DECL_CONTEXT (value));
  1225.       return void_type_node;
  1226.     }
  1227.  
  1228.   if (flag_cadillac)
  1229.     cadillac_start_decl (value);
  1230.  
  1231.   if (asmspec_tree)
  1232.     asmspec = TREE_STRING_POINTER (asmspec_tree);
  1233.  
  1234.   if (init)
  1235.     {
  1236.       if (IS_SIGNATURE (current_class_type)
  1237.       && TREE_CODE (value) == FUNCTION_DECL)
  1238.     {
  1239.       error ("function declarations cannot have initializers in signature");
  1240.       init = NULL_TREE;
  1241.     }
  1242.       else if (TREE_CODE (value) == FUNCTION_DECL)
  1243.     {
  1244.       grok_function_init (value, init);
  1245.       init = NULL_TREE;
  1246.     }
  1247.       else if (pedantic && TREE_CODE (value) != VAR_DECL)
  1248.     /* Already complained in grokdeclarator.  */
  1249.     init = NULL_TREE;
  1250.       else
  1251.     {
  1252.       /* We allow initializers to become parameters to base
  1253.              initializers.  */
  1254.       if (TREE_CODE (init) == TREE_LIST)
  1255.         {
  1256.           if (TREE_CHAIN (init) == NULL_TREE)
  1257.         init = TREE_VALUE (init);
  1258.           else
  1259.         init = digest_init (TREE_TYPE (value), init, (tree *)0);
  1260.         }
  1261.       
  1262.       if (TREE_CODE (init) == CONST_DECL)
  1263.         init = DECL_INITIAL (init);
  1264.       else if (TREE_READONLY_DECL_P (init))
  1265.         init = decl_constant_value (init);
  1266.       else if (TREE_CODE (init) == CONSTRUCTOR)
  1267.         init = digest_init (TREE_TYPE (value), init, (tree *)0);
  1268.       my_friendly_assert (TREE_PERMANENT (init), 192);
  1269.       if (init == error_mark_node)
  1270.         /* We must make this look different than `error_mark_node'
  1271.            because `decl_const_value' would mis-interpret it
  1272.            as only meaning that this VAR_DECL is defined.  */
  1273.         init = build1 (NOP_EXPR, TREE_TYPE (value), init);
  1274.       else if (! TREE_CONSTANT (init))
  1275.         {
  1276.           /* We can allow references to things that are effectively
  1277.          static, since references are initialized with the address.  */
  1278.           if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
  1279.           || (TREE_STATIC (init) == 0
  1280.               && (TREE_CODE_CLASS (TREE_CODE (init)) != 'd'
  1281.               || DECL_EXTERNAL (init) == 0)))
  1282.         {
  1283.           error ("field initializer is not constant");
  1284.           init = error_mark_node;
  1285.         }
  1286.         }
  1287.     }
  1288.     }
  1289.  
  1290.   /* The corresponding pop_obstacks is in finish_decl.  */
  1291.   push_obstacks_nochange ();
  1292.  
  1293.   if (TREE_CODE (value) == VAR_DECL)
  1294.     {
  1295.       /* We cannot call pushdecl here, because that would
  1296.      fill in the value of our TREE_CHAIN.  Instead, we
  1297.      modify finish_decl to do the right thing, namely, to
  1298.      put this decl out straight away.  */
  1299.       if (TREE_PUBLIC (value))
  1300.     {
  1301.       /* current_class_type can be NULL_TREE in case of error.  */
  1302.       if (asmspec == 0 && current_class_type)
  1303.         {
  1304.           tree name;
  1305.           char *buf, *buf2;
  1306.  
  1307.           buf2 = build_overload_name (current_class_type, 1, 1);
  1308.           buf = (char *)alloca (IDENTIFIER_LENGTH (DECL_NAME (value))
  1309.                     + sizeof (STATIC_NAME_FORMAT)
  1310.                     + strlen (buf2));
  1311.           sprintf (buf, STATIC_NAME_FORMAT, buf2,
  1312.                IDENTIFIER_POINTER (DECL_NAME (value)));
  1313.           name = get_identifier (buf);
  1314.           TREE_PUBLIC (value) = 1;
  1315.           DECL_INITIAL (value) = error_mark_node;
  1316.           DECL_ASSEMBLER_NAME (value) = name;
  1317.         }
  1318.       pending_statics = perm_tree_cons (NULL_TREE, value, pending_statics);
  1319.  
  1320.       /* Static consts need not be initialized in the class definition.  */
  1321.       if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (value)))
  1322.         {
  1323.           static int explanation = 0;
  1324.  
  1325.           error ("initializer invalid for static member with constructor");
  1326.           if (explanation++ == 0)
  1327.         error ("(you really want to initialize it separately)");
  1328.           init = 0;
  1329.         }
  1330.       /* Force the compiler to know when an uninitialized static
  1331.          const member is being used.  */
  1332.       if (TYPE_READONLY (value) && init == 0)
  1333.         TREE_USED (value) = 1;
  1334.     }
  1335.       DECL_INITIAL (value) = init;
  1336.       DECL_IN_AGGR_P (value) = 1;
  1337.  
  1338.       finish_decl (value, init, asmspec_tree, 1);
  1339.       pushdecl_class_level (value);
  1340.       return value;
  1341.     }
  1342.   if (TREE_CODE (value) == FIELD_DECL)
  1343.     {
  1344.       if (asmspec)
  1345.     DECL_ASSEMBLER_NAME (value) = get_identifier (asmspec);
  1346.       if (DECL_INITIAL (value) == error_mark_node)
  1347.     init = error_mark_node;
  1348.       finish_decl (value, init, asmspec_tree, 1);
  1349.       DECL_INITIAL (value) = init;
  1350.       DECL_IN_AGGR_P (value) = 1;
  1351.       return value;
  1352.     }
  1353.   if (TREE_CODE (value) == FUNCTION_DECL)
  1354.     {
  1355.       if (DECL_CHAIN (value) != NULL_TREE)
  1356.     {
  1357.       /* Need a fresh node here so that we don't get circularity
  1358.          when we link these together.  */
  1359.       value = copy_node (value);
  1360.       /* When does this happen?  */
  1361.       my_friendly_assert (init == NULL_TREE, 193);
  1362.     }
  1363.       finish_decl (value, init, asmspec_tree, 1);
  1364.  
  1365.       /* Pass friends back this way.  */
  1366.       if (DECL_FRIEND_P (value))
  1367.     return void_type_node;
  1368.  
  1369.       if (current_function_decl && ! IS_SIGNATURE (current_class_type))
  1370.     cp_error ("method `%#D' of local class must be defined in class body",
  1371.           value);
  1372.  
  1373.       DECL_IN_AGGR_P (value) = 1;
  1374.       return value;
  1375.     }
  1376.   my_friendly_abort (21);
  1377.   /* NOTREACHED */
  1378.   return NULL_TREE;
  1379. }
  1380.  
  1381. /* Like `grokfield', but for bitfields.
  1382.    WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.  */
  1383.  
  1384. tree
  1385. grokbitfield (declarator, declspecs, width)
  1386.      tree declarator, declspecs, width;
  1387. {
  1388.   register tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, NULL_TREE);
  1389.  
  1390.   if (! value) return NULL_TREE; /* friends went bad.  */
  1391.  
  1392.   /* Pass friendly classes back.  */
  1393.   if (TREE_CODE (value) == VOID_TYPE)
  1394.     return void_type_node;
  1395.  
  1396.   if (TREE_CODE (value) == TYPE_DECL)
  1397.     {
  1398.       cp_error ("cannot declare `%D' to be a bitfield type", value);
  1399.       return NULL_TREE;
  1400.     }
  1401.  
  1402.   if (IS_SIGNATURE (current_class_type))
  1403.     {
  1404.       error ("field declaration not allowed in signature");
  1405.       return void_type_node;
  1406.     }
  1407.  
  1408.   if (DECL_IN_AGGR_P (value))
  1409.     {
  1410.       cp_error ("`%D' is already defined in the class %T", value,
  1411.           DECL_CONTEXT (value));
  1412.       return void_type_node;
  1413.     }
  1414.  
  1415.   GNU_xref_member (current_class_name, value);
  1416.  
  1417.   if (TREE_STATIC (value))
  1418.     {
  1419.       cp_error ("static member `%D' cannot be a bitfield", value);
  1420.       return NULL_TREE;
  1421.     }
  1422.   finish_decl (value, NULL_TREE, NULL_TREE, 0);
  1423.  
  1424.   if (width != error_mark_node)
  1425.     {
  1426.       /* detect invalid field size.  */
  1427.       if (TREE_CODE (width) == CONST_DECL)
  1428.     width = DECL_INITIAL (width);
  1429.       else if (TREE_READONLY_DECL_P (width))
  1430.     width = decl_constant_value (width);
  1431.       if (TREE_CODE (width) != INTEGER_CST)
  1432.     {
  1433.       cp_error ("structure field `%D' width not an integer constant",
  1434.               value);
  1435.       DECL_INITIAL (value) = NULL_TREE;
  1436.     }
  1437.       else
  1438.     {
  1439.       constant_expression_warning (width);
  1440.       DECL_INITIAL (value) = width;
  1441.       DECL_BIT_FIELD (value) = 1;
  1442.     }
  1443.     }
  1444.  
  1445.   DECL_IN_AGGR_P (value) = 1;
  1446.   return value;
  1447. }
  1448.  
  1449. #if 0
  1450. /* Like GROKFIELD, except that the declarator has been
  1451.    buried in DECLSPECS.  Find the declarator, and
  1452.    return something that looks like it came from
  1453.    GROKFIELD.  */
  1454. tree
  1455. groktypefield (declspecs, parmlist)
  1456.      tree declspecs;
  1457.      tree parmlist;
  1458. {
  1459.   tree spec = declspecs;
  1460.   tree prev = NULL_TREE;
  1461.  
  1462.   tree type_id = NULL_TREE;
  1463.   tree quals = NULL_TREE;
  1464.   tree lengths = NULL_TREE;
  1465.   tree decl = NULL_TREE;
  1466.  
  1467.   while (spec)
  1468.     {
  1469.       register tree id = TREE_VALUE (spec);
  1470.  
  1471.       if (TREE_CODE (spec) != TREE_LIST)
  1472.     /* Certain parse errors slip through.  For example,
  1473.        `int class ();' is not caught by the parser. Try
  1474.        weakly to recover here.  */
  1475.     return NULL_TREE;
  1476.  
  1477.       if (TREE_CODE (id) == TYPE_DECL
  1478.       || (TREE_CODE (id) == IDENTIFIER_NODE && TREE_TYPE (id)))
  1479.     {
  1480.       /* We have a constructor/destructor or
  1481.          conversion operator.  Use it.  */
  1482.       if (prev)
  1483.         TREE_CHAIN (prev) = TREE_CHAIN (spec);
  1484.       else
  1485.         declspecs = TREE_CHAIN (spec);
  1486.  
  1487.       type_id = id;
  1488.       goto found;
  1489.     }
  1490.       prev = spec;
  1491.       spec = TREE_CHAIN (spec);
  1492.     }
  1493.  
  1494.   /* Nope, we have a conversion operator to a scalar type or something
  1495.      else, that includes things like constructor declarations for
  1496.      templates.  */
  1497.   spec = declspecs;
  1498.   while (spec)
  1499.     {
  1500.       tree id = TREE_VALUE (spec);
  1501.  
  1502.       if (TREE_CODE (id) == IDENTIFIER_NODE)
  1503.     {
  1504.       if (id == ridpointers[(int)RID_INT]
  1505.           || id == ridpointers[(int)RID_DOUBLE]
  1506.           || id == ridpointers[(int)RID_FLOAT]
  1507.           || id == ridpointers[(int)RID_WCHAR])
  1508.         {
  1509.           if (type_id)
  1510.         error ("extra `%s' ignored",
  1511.                IDENTIFIER_POINTER (id));
  1512.           else
  1513.         type_id = id;
  1514.         }
  1515.       else if (id == ridpointers[(int)RID_LONG]
  1516.            || id == ridpointers[(int)RID_SHORT]
  1517.            || id == ridpointers[(int)RID_CHAR])
  1518.         {
  1519.           lengths = tree_cons (NULL_TREE, id, lengths);
  1520.         }
  1521.       else if (id == ridpointers[(int)RID_VOID])
  1522.         {
  1523.           if (type_id)
  1524.         error ("spurious `void' type ignored");
  1525.           else
  1526.         error ("conversion to `void' type invalid");
  1527.         }
  1528.       else if (id == ridpointers[(int)RID_AUTO]
  1529.            || id == ridpointers[(int)RID_REGISTER]
  1530.            || id == ridpointers[(int)RID_TYPEDEF]
  1531.            || id == ridpointers[(int)RID_CONST]
  1532.            || id == ridpointers[(int)RID_VOLATILE])
  1533.         {
  1534.           error ("type specifier `%s' used invalidly",
  1535.              IDENTIFIER_POINTER (id));
  1536.         }
  1537.       else if (id == ridpointers[(int)RID_FRIEND]
  1538.            || id == ridpointers[(int)RID_VIRTUAL]
  1539.            || id == ridpointers[(int)RID_INLINE]
  1540.            || id == ridpointers[(int)RID_UNSIGNED]
  1541.            || id == ridpointers[(int)RID_SIGNED]
  1542.            || id == ridpointers[(int)RID_STATIC]
  1543.            || id == ridpointers[(int)RID_EXTERN])
  1544.         {
  1545.           quals = tree_cons (NULL_TREE, id, quals);
  1546.         }
  1547.       else
  1548.         {
  1549.           /* Happens when we have a global typedef
  1550.          and a class-local member function with
  1551.          the same name.  */
  1552.           type_id = id;
  1553.           goto found;
  1554.         }
  1555.     }
  1556.       else if (TREE_CODE (id) == RECORD_TYPE)
  1557.     {
  1558.       type_id = TYPE_NAME (id);
  1559.       if (TREE_CODE (type_id) == TYPE_DECL)
  1560.         type_id = DECL_NAME (type_id);
  1561.       if (type_id == NULL_TREE)
  1562.         error ("identifier for aggregate type conversion omitted");
  1563.     }
  1564.       else if (TREE_CODE_CLASS (TREE_CODE (id)) == 't')
  1565.     error ("`operator' missing on conversion operator or tag missing from type");
  1566.       else
  1567.     my_friendly_abort (194);
  1568.       spec = TREE_CHAIN (spec);
  1569.     }
  1570.  
  1571.   if (type_id)
  1572.     declspecs = chainon (lengths, quals);
  1573.   else if (lengths)
  1574.     {
  1575.       if (TREE_CHAIN (lengths))
  1576.     error ("multiple length specifiers");
  1577.       type_id = ridpointers[(int)RID_INT];
  1578.       declspecs = chainon (lengths, quals);
  1579.     }
  1580.   else if (quals)
  1581.     {
  1582.       error ("no type given, defaulting to `operator int ...'");
  1583.       type_id = ridpointers[(int)RID_INT];
  1584.       declspecs = quals;
  1585.     }
  1586.   else
  1587.     return NULL_TREE;
  1588.  
  1589.  found:
  1590.   decl = grokdeclarator (build_parse_node (CALL_EXPR, type_id, parmlist, NULL_TREE),
  1591.              declspecs, FIELD, 0, NULL_TREE);
  1592.   if (decl == NULL_TREE)
  1593.     return NULL_TREE;
  1594.  
  1595.   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_CHAIN (decl) != NULL_TREE)
  1596.     {
  1597.       /* Need a fresh node here so that we don't get circularity
  1598.      when we link these together.  */
  1599.       decl = copy_node (decl);
  1600.     }
  1601.  
  1602.   if (decl == void_type_node
  1603.       || (TREE_CODE (decl) == FUNCTION_DECL
  1604.       && TREE_CODE (TREE_TYPE (decl)) != METHOD_TYPE))
  1605.     /* bunch of friends.  */
  1606.     return decl;
  1607.  
  1608.   if (DECL_IN_AGGR_P (decl))
  1609.     {
  1610.       cp_error ("`%D' already defined in the class ", decl);
  1611.       return void_type_node;
  1612.     }
  1613.  
  1614.   finish_decl (decl, NULL_TREE, NULL_TREE, 0);
  1615.  
  1616.   /* If this declaration is common to another declaration
  1617.      complain about such redundancy, and return NULL_TREE
  1618.      so that we don't build a circular list.  */
  1619.   if (DECL_CHAIN (decl))
  1620.     {
  1621.       cp_error ("function `%D' declared twice in class %T", decl,
  1622.           DECL_CONTEXT (decl));
  1623.       return NULL_TREE;
  1624.     }
  1625.   DECL_IN_AGGR_P (decl) = 1;
  1626.   return decl;
  1627. }
  1628. #endif
  1629.  
  1630. tree
  1631. grokoptypename (declspecs, declarator)
  1632.      tree declspecs, declarator;
  1633. {
  1634.   tree t = grokdeclarator (declarator, declspecs, TYPENAME, 0, NULL_TREE);
  1635.   return build_typename_overload (t);
  1636. }
  1637.  
  1638. /* When a function is declared with an initializer,
  1639.    do the right thing.  Currently, there are two possibilities:
  1640.  
  1641.    class B
  1642.    {
  1643.     public:
  1644.      // initialization possibility #1.
  1645.      virtual void f () = 0;
  1646.      int g ();
  1647.    };
  1648.    
  1649.    class D1 : B
  1650.    {
  1651.     public:
  1652.      int d1;
  1653.      // error, no f ();
  1654.    };
  1655.    
  1656.    class D2 : B
  1657.    {
  1658.     public:
  1659.      int d2;
  1660.      void f ();
  1661.    };
  1662.    
  1663.    class D3 : B
  1664.    {
  1665.     public:
  1666.      int d3;
  1667.      // initialization possibility #2
  1668.      void f () = B::f;
  1669.    };
  1670.  
  1671. */
  1672.  
  1673. int
  1674. copy_assignment_arg_p (parmtype, virtualp)
  1675.      tree parmtype;
  1676.      int virtualp;
  1677. {
  1678.   if (TREE_CODE (parmtype) == REFERENCE_TYPE)
  1679.     parmtype = TREE_TYPE (parmtype);
  1680.  
  1681.   if ((TYPE_MAIN_VARIANT (parmtype) == current_class_type)
  1682.       || (virtualp && DERIVED_FROM_P (parmtype, current_class_type)))
  1683.     return 1;
  1684.  
  1685.   return 0;
  1686. }
  1687.  
  1688. static void
  1689. grok_function_init (decl, init)
  1690.      tree decl;
  1691.      tree init;
  1692. {
  1693.   /* An initializer for a function tells how this function should
  1694.      be inherited.  */
  1695.   tree type = TREE_TYPE (decl);
  1696.  
  1697.   if (TREE_CODE (type) == FUNCTION_TYPE)
  1698.     cp_error ("initializer specified for non-member function `%D'", decl);
  1699.   else if (DECL_VINDEX (decl) == NULL_TREE)
  1700.     cp_error ("initializer specified for non-virtual method `%D'", decl);
  1701.   else if (integer_zerop (init))
  1702.     {
  1703. #if 0
  1704.       /* Mark this function as being "defined".  */
  1705.       DECL_INITIAL (decl) = error_mark_node;
  1706.       /* pure virtual destructors must be defined. */
  1707.       /* pure virtual needs to be defined (as abort) only when put in 
  1708.      vtbl. For wellformed call, it should be itself. pr4737 */
  1709.       if (!DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)))
  1710.     {
  1711.       extern tree abort_fndecl;
  1712.       /* Give this node rtl from `abort'.  */
  1713.       DECL_RTL (decl) = DECL_RTL (abort_fndecl);
  1714.     }
  1715. #endif
  1716.       DECL_ABSTRACT_VIRTUAL_P (decl) = 1;
  1717.       if (DECL_NAME (decl) == ansi_opname [(int) MODIFY_EXPR])
  1718.     {
  1719.       tree parmtype
  1720.         = TREE_VALUE (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))));
  1721.  
  1722.       if (copy_assignment_arg_p (parmtype, 1))
  1723.         TYPE_HAS_ABSTRACT_ASSIGN_REF (current_class_type) = 1;
  1724.     }
  1725.     }
  1726.   else if (TREE_CODE (init) == OFFSET_REF
  1727.        && TREE_OPERAND (init, 0) == NULL_TREE
  1728.        && TREE_CODE (TREE_TYPE (init)) == METHOD_TYPE)
  1729.     {
  1730.       tree basetype = DECL_CLASS_CONTEXT (init);
  1731.       tree basefn = TREE_OPERAND (init, 1);
  1732.       if (TREE_CODE (basefn) != FUNCTION_DECL)
  1733.     cp_error ("non-method initializer invalid for method `%D'", decl);
  1734.       else if (! BINFO_OFFSET_ZEROP (TYPE_BINFO (DECL_CLASS_CONTEXT (basefn))))
  1735.     sorry ("base member function from other than first base class");
  1736.       else
  1737.     {
  1738.       tree binfo = get_binfo (basetype, TYPE_METHOD_BASETYPE (type), 1);
  1739.       if (binfo == error_mark_node)
  1740.         ;
  1741.       else if (binfo == 0)
  1742.         error_not_base_type (TYPE_METHOD_BASETYPE (TREE_TYPE (init)),
  1743.                  TYPE_METHOD_BASETYPE (type));
  1744.       else
  1745.         {
  1746.           /* Mark this function as being defined,
  1747.          and give it new rtl.  */
  1748.           DECL_INITIAL (decl) = error_mark_node;
  1749.           DECL_RTL (decl) = DECL_RTL (basefn);
  1750.         }
  1751.     }
  1752.     }
  1753.   else
  1754.     cp_error ("invalid initializer for virtual method `%D'", decl);
  1755. }
  1756.  
  1757. /* When we get a declaration of the form
  1758.  
  1759.    type cname::fname ...
  1760.  
  1761.    the node for `cname::fname' gets built here in a special way.
  1762.    Namely, we push into `cname's scope.  When this declaration is
  1763.    processed, we pop back out.  */
  1764. tree
  1765. build_push_scope (cname, name)
  1766.      tree cname;
  1767.      tree name;
  1768. {
  1769.   extern int current_class_depth;
  1770.   tree ctype, rval;
  1771.   int is_ttp = 0;
  1772.  
  1773.   if (cname == error_mark_node)
  1774.     return error_mark_node;
  1775.  
  1776.   ctype = IDENTIFIER_TYPE_VALUE (cname);
  1777.  
  1778.   if (TREE_CODE (ctype) == TEMPLATE_TYPE_PARM)
  1779.     is_ttp = 1;
  1780.   else if (ctype == NULL_TREE || ! IS_AGGR_TYPE (ctype))
  1781.     {
  1782.       cp_error ("`%T' not defined as aggregate type", cname);
  1783.       return name;
  1784.     }
  1785.   else if (IS_SIGNATURE (ctype))
  1786.     {
  1787.       error ("cannot push into signature scope, scope resolution operator ignored");
  1788.       return name;
  1789.     }
  1790.  
  1791.   rval = build_parse_node (SCOPE_REF, cname, name);
  1792.  
  1793.   /* Don't need to push the scope if we're already in it.
  1794.      We also don't need to push the scope for a ptr-to-member/method.  */
  1795.  
  1796.   if (ctype == current_class_type || TREE_CODE (name) != IDENTIFIER_NODE
  1797.       || is_ttp)
  1798.     return rval;
  1799.  
  1800.   /* We do need to push the scope in this case, since CTYPE helps
  1801.      determine subsequent intializers (i.e., Foo::Bar x = foo_enum_1;).  */
  1802.  
  1803.   push_nested_class (ctype, 3);
  1804.   TREE_COMPLEXITY (rval) = current_class_depth;
  1805.   return rval;
  1806. }
  1807.  
  1808. void cplus_decl_attributes (decl, attributes)
  1809.      tree decl, attributes;
  1810. {
  1811.   if (decl && decl != void_type_node)
  1812.     decl_attributes (decl, attributes);
  1813. }
  1814.  
  1815. /* CONSTRUCTOR_NAME:
  1816.    Return the name for the constructor (or destructor) for the
  1817.    specified class.  Argument can be RECORD_TYPE, TYPE_DECL, or
  1818.    IDENTIFIER_NODE.  When given a template, this routine doesn't
  1819.    lose the specialization.  */
  1820. tree
  1821. constructor_name_full (thing)
  1822.      tree thing;
  1823. {
  1824.   if (TREE_CODE (thing) == UNINSTANTIATED_P_TYPE)
  1825.     return DECL_NAME (UPT_TEMPLATE (thing));
  1826.   if (IS_AGGR_TYPE_CODE (TREE_CODE (thing)))
  1827.     {
  1828.       if (TYPE_WAS_ANONYMOUS (thing) && TYPE_HAS_CONSTRUCTOR (thing))
  1829.     thing = DECL_NAME (TREE_VEC_ELT (TYPE_METHODS (thing), 0));
  1830.       else
  1831.     thing = TYPE_NAME (thing);
  1832.     }
  1833.   if (TREE_CODE (thing) == TYPE_DECL
  1834.       || (TREE_CODE (thing) == TEMPLATE_DECL
  1835.       && DECL_TEMPLATE_IS_CLASS (thing)))
  1836.     thing = DECL_NAME (thing);
  1837.   my_friendly_assert (TREE_CODE (thing) == IDENTIFIER_NODE, 197);
  1838.   return thing;
  1839. }
  1840.  
  1841. /* CONSTRUCTOR_NAME:
  1842.    Return the name for the constructor (or destructor) for the
  1843.    specified class.  Argument can be RECORD_TYPE, TYPE_DECL, or
  1844.    IDENTIFIER_NODE.  When given a template, return the plain
  1845.    unspecialized name.  */
  1846. tree
  1847. constructor_name (thing)
  1848.      tree thing;
  1849. {
  1850.   tree t;
  1851.   thing = constructor_name_full (thing);
  1852.   t = IDENTIFIER_TEMPLATE (thing);
  1853.   if (!t)
  1854.     return thing;
  1855.   t = TREE_PURPOSE (t);
  1856.   return DECL_NAME (t);
  1857. }
  1858.  
  1859. /* Cache the value of this class's main virtual function table pointer
  1860.    in a register variable.  This will save one indirection if a
  1861.    more than one virtual function call is made this function.  */
  1862. void
  1863. setup_vtbl_ptr ()
  1864. {
  1865.   extern rtx base_init_insns;
  1866.  
  1867.   if (base_init_insns == 0
  1868.       && DECL_CONSTRUCTOR_P (current_function_decl))
  1869.     emit_base_init (current_class_type, 0);
  1870.  
  1871. #if 0
  1872.   /* This has something a little wrong with it.
  1873.  
  1874.      On a sun4, code like:
  1875.  
  1876.         be L6
  1877.         ld [%i0],%o1
  1878.  
  1879.      is generated, when the below is used when -O4 is given.  The delay
  1880.      slot it filled with an instruction that is safe, when this isn't
  1881.      used, like in:
  1882.  
  1883.         be L6
  1884.         sethi %hi(LC1),%o0
  1885.         ld [%i0],%o1
  1886.  
  1887.      on code like:
  1888.  
  1889.         struct A {
  1890.           virtual void print() { printf("xxx"); }
  1891.           void f();
  1892.         };
  1893.  
  1894.         void A::f() {
  1895.           if (this) {
  1896.             print();
  1897.           } else {
  1898.             printf("0");
  1899.           }
  1900.         }
  1901.  
  1902.      And that is why this is disabled for now. (mrs)
  1903.   */
  1904.  
  1905.   if ((flag_this_is_variable & 1) == 0
  1906.       && optimize
  1907.       && current_class_type
  1908.       && CLASSTYPE_VSIZE (current_class_type)
  1909.       && ! DECL_STATIC_FUNCTION_P (current_function_decl))
  1910.     {
  1911.       tree vfield = build_vfield_ref (C_C_D, current_class_type);
  1912.       current_vtable_decl = CLASSTYPE_VTBL_PTR (current_class_type);
  1913.       DECL_RTL (current_vtable_decl) = 0;
  1914.       DECL_INITIAL (current_vtable_decl) = error_mark_node;
  1915.       /* Have to cast the initializer, since it may have come from a
  1916.      more base class then we ascribe CURRENT_VTABLE_DECL to be.  */
  1917.       finish_decl (current_vtable_decl, convert_force (TREE_TYPE (current_vtable_decl), vfield), 0, 0);
  1918.       current_vtable_decl = build_indirect_ref (current_vtable_decl, NULL_PTR);
  1919.     }
  1920.   else
  1921. #endif
  1922.     current_vtable_decl = NULL_TREE;
  1923. }
  1924.  
  1925. /* Record the existence of an addressable inline function.  */
  1926. void
  1927. mark_inline_for_output (decl)
  1928.      tree decl;
  1929. {
  1930.   if (DECL_SAVED_INLINE (decl))
  1931.     return;
  1932.   DECL_SAVED_INLINE (decl) = 1;
  1933.   if (DECL_PENDING_INLINE_INFO (decl) != 0
  1934.       && ! DECL_PENDING_INLINE_INFO (decl)->deja_vu)
  1935.     {
  1936.       struct pending_inline *t = pending_inlines;
  1937.       my_friendly_assert (DECL_SAVED_INSNS (decl) == 0, 198);
  1938.       while (t)
  1939.     {
  1940.       if (t == DECL_PENDING_INLINE_INFO (decl))
  1941.         break;
  1942.       t = t->next;
  1943.     }
  1944.       if (t == 0)
  1945.     {
  1946.       t = DECL_PENDING_INLINE_INFO (decl);
  1947.       t->next = pending_inlines;
  1948.       pending_inlines = t;
  1949.     }
  1950.       DECL_PENDING_INLINE_INFO (decl) = 0;
  1951.     }
  1952.   saved_inlines = perm_tree_cons (NULL_TREE, decl, saved_inlines);
  1953. }
  1954.  
  1955. void
  1956. clear_temp_name ()
  1957. {
  1958.   temp_name_counter = 0;
  1959. }
  1960.  
  1961. /* Hand off a unique name which can be used for variable we don't really
  1962.    want to know about anyway, for example, the anonymous variables which
  1963.    are needed to make references work.  Declare this thing so we can use it.
  1964.    The variable created will be of type TYPE.
  1965.  
  1966.    STATICP is nonzero if this variable should be static.  */
  1967.  
  1968. tree
  1969. get_temp_name (type, staticp)
  1970.      tree type;
  1971.      int staticp;
  1972. {
  1973.   char buf[sizeof (AUTO_TEMP_FORMAT) + 20];
  1974.   tree decl;
  1975.   int toplev = global_bindings_p ();
  1976.  
  1977.   push_obstacks_nochange ();
  1978.   if (toplev || staticp)
  1979.     {
  1980.       end_temporary_allocation ();
  1981.       sprintf (buf, AUTO_TEMP_FORMAT, global_temp_name_counter++);
  1982.       decl = pushdecl_top_level (build_decl (VAR_DECL, get_identifier (buf), type));
  1983.     }
  1984.   else
  1985.     {
  1986.       sprintf (buf, AUTO_TEMP_FORMAT, temp_name_counter++);
  1987.       decl = pushdecl (build_decl (VAR_DECL, get_identifier (buf), type));
  1988.     }
  1989.   TREE_USED (decl) = 1;
  1990.   TREE_STATIC (decl) = staticp;
  1991.  
  1992.   /* If this is a local variable, then lay out its rtl now.
  1993.      Otherwise, callers of this function are responsible for dealing
  1994.      with this variable's rtl.  */
  1995.   if (! toplev)
  1996.     {
  1997.       expand_decl (decl);
  1998.       expand_decl_init (decl);
  1999.     }
  2000.   pop_obstacks ();
  2001.  
  2002.   return decl;
  2003. }
  2004.  
  2005. /* Get a variable which we can use for multiple assignments.
  2006.    It is not entered into current_binding_level, because
  2007.    that breaks things when it comes time to do final cleanups
  2008.    (which take place "outside" the binding contour of the function).  */
  2009. tree
  2010. get_temp_regvar (type, init)
  2011.      tree type, init;
  2012. {
  2013.   static char buf[sizeof (AUTO_TEMP_FORMAT) + 20] = { '_' };
  2014.   tree decl;
  2015.  
  2016.   sprintf (buf+1, AUTO_TEMP_FORMAT, temp_name_counter++);
  2017.   decl = build_decl (VAR_DECL, get_identifier (buf), type);
  2018.   TREE_USED (decl) = 1;
  2019.   DECL_REGISTER (decl) = 1;
  2020.  
  2021.   if (init)
  2022.     store_init_value (decl, init);
  2023.  
  2024.   /* We can expand these without fear, since they cannot need
  2025.      constructors or destructors.  */
  2026.   expand_decl (decl);
  2027.   expand_decl_init (decl);
  2028.  
  2029.   if (type_needs_gc_entry (type))
  2030.     DECL_GC_OFFSET (decl) = size_int (++current_function_obstack_index);
  2031.  
  2032.   return decl;
  2033. }
  2034.  
  2035. /* Make the macro TEMP_NAME_P available to units which do not
  2036.    include c-tree.h.  */
  2037. int
  2038. temp_name_p (decl)
  2039.      tree decl;
  2040. {
  2041.   return TEMP_NAME_P (decl);
  2042. }
  2043.  
  2044. /* Finish off the processing of a UNION_TYPE structure.
  2045.    If there are static members, then all members are
  2046.    static, and must be laid out together.  If the
  2047.    union is an anonymous union, we arrange for that
  2048.    as well.  PUBLIC_P is nonzero if this union is
  2049.    not declared static.  */
  2050. void
  2051. finish_anon_union (anon_union_decl)
  2052.      tree anon_union_decl;
  2053. {
  2054.   tree type = TREE_TYPE (anon_union_decl);
  2055.   tree field, main_decl = NULL_TREE;
  2056.   tree elems = NULL_TREE;
  2057.   int public_p = TREE_PUBLIC (anon_union_decl);
  2058.   int static_p = TREE_STATIC (anon_union_decl);
  2059.   int external_p = DECL_EXTERNAL (anon_union_decl);
  2060.  
  2061.   if ((field = TYPE_FIELDS (type)) == NULL_TREE)
  2062.     return;
  2063.  
  2064.   if (public_p)
  2065.     {
  2066.       error ("global anonymous unions must be declared static");
  2067.       return;
  2068.     }
  2069.  
  2070.   while (field)
  2071.     {
  2072.       tree decl = build_decl (VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
  2073.       /* tell `pushdecl' that this is not tentative.  */
  2074.       DECL_INITIAL (decl) = error_mark_node;
  2075.       TREE_PUBLIC (decl) = public_p;
  2076.       TREE_STATIC (decl) = static_p;
  2077.       DECL_EXTERNAL (decl) = external_p;
  2078.       decl = pushdecl (decl);
  2079.  
  2080.       /* Only write out one anon union element--choose the one that
  2081.      can hold them all.  */
  2082.       if (main_decl == NULL_TREE
  2083.       && simple_cst_equal (DECL_SIZE (decl), DECL_SIZE (anon_union_decl)))
  2084.     {
  2085.       main_decl = decl;
  2086.     }
  2087.       else
  2088.     {
  2089.       /* ??? This causes there to be no debug info written out
  2090.          about this decl.  */
  2091.       TREE_ASM_WRITTEN (decl) = 1;
  2092.     }
  2093.  
  2094.       DECL_INITIAL (decl) = NULL_TREE;
  2095.       /* If there's a cleanup to do, it belongs in the
  2096.      TREE_PURPOSE of the following TREE_LIST.  */
  2097.       elems = tree_cons (NULL_TREE, decl, elems);
  2098.       TREE_TYPE (elems) = type;
  2099.       field = TREE_CHAIN (field);
  2100.     }
  2101.   if (static_p)
  2102.     {
  2103.       make_decl_rtl (main_decl, 0, global_bindings_p ());
  2104.       DECL_RTL (anon_union_decl) = DECL_RTL (main_decl);
  2105.     }
  2106.  
  2107.   /* The following call assumes that there are never any cleanups
  2108.      for anonymous unions--a reasonable assumption.  */
  2109.   expand_anon_union_decl (anon_union_decl, NULL_TREE, elems);
  2110.  
  2111.   if (flag_cadillac)
  2112.     cadillac_finish_anon_union (anon_union_decl);
  2113. }
  2114.  
  2115. /* Finish and output a table which is generated by the compiler.
  2116.    NAME is the name to give the table.
  2117.    TYPE is the type of the table entry.
  2118.    INIT is all the elements in the table.
  2119.    PUBLICP is non-zero if this table should be given external access.  */
  2120. tree
  2121. finish_table (name, type, init, publicp)
  2122.      tree name, type, init;
  2123.      int publicp;
  2124. {
  2125.   tree itype, atype, decl;
  2126.   static tree empty_table;
  2127.   int is_empty = 0;
  2128.   tree asmspec;
  2129.  
  2130.   itype = build_index_type (size_int (list_length (init) - 1));
  2131.   atype = build_cplus_array_type (type, itype);
  2132.   layout_type (atype);
  2133.  
  2134.   if (TREE_VALUE (init) == integer_zero_node
  2135.       && TREE_CHAIN (init) == NULL_TREE)
  2136.     {
  2137.       if (empty_table == NULL_TREE)
  2138.     {
  2139.       empty_table = get_temp_name (atype, 1);
  2140.       init = build (CONSTRUCTOR, atype, NULL_TREE, init);
  2141.       TREE_CONSTANT (init) = 1;
  2142.       TREE_STATIC (init) = 1;
  2143.       DECL_INITIAL (empty_table) = init;
  2144.       asmspec = build_string (IDENTIFIER_LENGTH (DECL_NAME (empty_table)),
  2145.                   IDENTIFIER_POINTER (DECL_NAME (empty_table)));
  2146.       finish_decl (empty_table, init, asmspec, 0);
  2147.     }
  2148.       is_empty = 1;
  2149.     }
  2150.  
  2151.   if (name == NULL_TREE)
  2152.     {
  2153.       if (is_empty)
  2154.     return empty_table;
  2155.       decl = get_temp_name (atype, 1);
  2156.     }
  2157.   else
  2158.     {
  2159.       decl = build_decl (VAR_DECL, name, atype);
  2160.       decl = pushdecl (decl);
  2161.       TREE_STATIC (decl) = 1;
  2162.     }
  2163.  
  2164.   if (is_empty == 0)
  2165.     {
  2166.       TREE_PUBLIC (decl) = publicp;
  2167.       init = build (CONSTRUCTOR, atype, NULL_TREE, init);
  2168.       TREE_CONSTANT (init) = 1;
  2169.       TREE_STATIC (init) = 1;
  2170.       DECL_INITIAL (decl) = init;
  2171.       asmspec = build_string (IDENTIFIER_LENGTH (DECL_NAME (decl)),
  2172.                   IDENTIFIER_POINTER (DECL_NAME (decl)));
  2173.     }
  2174.   else
  2175.     {
  2176.       /* This will cause DECL to point to EMPTY_TABLE in rtl-land.  */
  2177.       DECL_EXTERNAL (decl) = 1;
  2178.       TREE_STATIC (decl) = 0;
  2179.       init = 0;
  2180.       asmspec = build_string (IDENTIFIER_LENGTH (DECL_NAME (empty_table)),
  2181.                   IDENTIFIER_POINTER (DECL_NAME (empty_table)));
  2182.     }
  2183.  
  2184.   finish_decl (decl, init, asmspec, 0);
  2185.   return decl;
  2186. }
  2187.  
  2188. /* Finish processing a builtin type TYPE.  It's name is NAME,
  2189.    its fields are in the array FIELDS.  LEN is the number of elements
  2190.    in FIELDS minus one, or put another way, it is the maximum subscript
  2191.    used in FIELDS.
  2192.  
  2193.    It is given the same alignment as ALIGN_TYPE.  */
  2194. void
  2195. finish_builtin_type (type, name, fields, len, align_type)
  2196.      tree type;
  2197.      char *name;
  2198.      tree fields[];
  2199.      int len;
  2200.      tree align_type;
  2201. {
  2202.   register int i;
  2203.  
  2204.   TYPE_FIELDS (type) = fields[0];
  2205.   for (i = 0; i < len; i++)
  2206.     {
  2207.       layout_type (TREE_TYPE (fields[i]));
  2208.       DECL_FIELD_CONTEXT (fields[i]) = type;
  2209.       TREE_CHAIN (fields[i]) = fields[i+1];
  2210.     }
  2211.   DECL_FIELD_CONTEXT (fields[i]) = type;
  2212.   DECL_CLASS_CONTEXT (fields[i]) = type;
  2213.   TYPE_ALIGN (type) = TYPE_ALIGN (align_type);
  2214.   layout_type (type);
  2215. #if 0 /* not yet, should get fixed properly later */
  2216.   TYPE_NAME (type) = make_type_decl (get_identifier (name), type);
  2217. #else
  2218.   TYPE_NAME (type) = build_decl (TYPE_DECL, get_identifier (name), type);
  2219. #endif
  2220.   layout_decl (TYPE_NAME (type), 0);
  2221. }
  2222.  
  2223. /* Auxiliary functions to make type signatures for
  2224.    `operator new' and `operator delete' correspond to
  2225.    what compiler will be expecting.  */
  2226.  
  2227. extern tree sizetype;
  2228.  
  2229. tree
  2230. coerce_new_type (type)
  2231.      tree type;
  2232. {
  2233.   int e1 = 0, e2 = 0;
  2234.  
  2235.   if (TREE_CODE (type) == METHOD_TYPE)
  2236.     type = build_function_type (TREE_TYPE (type), TREE_CHAIN (TYPE_ARG_TYPES (type)));
  2237.   if (TREE_TYPE (type) != ptr_type_node)
  2238.     e1 = 1, error ("`operator new' must return type `void *'");
  2239.  
  2240.   /* Technically the type must be `size_t', but we may not know
  2241.      what that is.  */
  2242.   if (TYPE_ARG_TYPES (type) == NULL_TREE)
  2243.     e1 = 1, error ("`operator new' takes type `size_t' parameter");
  2244.   else if (TREE_CODE (TREE_VALUE (TYPE_ARG_TYPES (type))) != INTEGER_TYPE
  2245.        || TYPE_PRECISION (TREE_VALUE (TYPE_ARG_TYPES (type))) != TYPE_PRECISION (sizetype))
  2246.     e2 = 1, error ("`operator new' takes type `size_t' as first parameter");
  2247.   if (e2)
  2248.     type = build_function_type (ptr_type_node, tree_cons (NULL_TREE, sizetype, TREE_CHAIN (TYPE_ARG_TYPES (type))));
  2249.   else if (e1)
  2250.     type = build_function_type (ptr_type_node, TYPE_ARG_TYPES (type));
  2251.   return type;
  2252. }
  2253.  
  2254. tree
  2255. coerce_delete_type (type)
  2256.      tree type;
  2257. {
  2258.   int e1 = 0, e2 = 0, e3 = 0;
  2259.   tree arg_types = TYPE_ARG_TYPES (type);
  2260.  
  2261.   if (TREE_CODE (type) == METHOD_TYPE)
  2262.     {
  2263.       type = build_function_type (TREE_TYPE (type), TREE_CHAIN (arg_types));
  2264.       arg_types = TREE_CHAIN (arg_types);
  2265.     }
  2266.   if (TREE_TYPE (type) != void_type_node)
  2267.     e1 = 1, error ("`operator delete' must return type `void'");
  2268.   if (arg_types == NULL_TREE
  2269.       || TREE_VALUE (arg_types) != ptr_type_node)
  2270.     e2 = 1, error ("`operator delete' takes type `void *' as first parameter");
  2271.  
  2272.   if (arg_types
  2273.       && TREE_CHAIN (arg_types)
  2274.       && TREE_CHAIN (arg_types) != void_list_node)
  2275.     {
  2276.       /* Again, technically this argument must be `size_t', but again
  2277.      we may not know what that is.  */
  2278.       tree t2 = TREE_VALUE (TREE_CHAIN (arg_types));
  2279.       if (TREE_CODE (t2) != INTEGER_TYPE
  2280.       || TYPE_PRECISION (t2) != TYPE_PRECISION (sizetype))
  2281.     e3 = 1, error ("second argument to `operator delete' must be of type `size_t'");
  2282.       else if (TREE_CHAIN (TREE_CHAIN (arg_types)) != void_list_node)
  2283.     {
  2284.       e3 = 1;
  2285.       if (TREE_CHAIN (TREE_CHAIN (arg_types)))
  2286.         error ("too many arguments in declaration of `operator delete'");
  2287.       else
  2288.         error ("`...' invalid in specification of `operator delete'");
  2289.     }
  2290.     }
  2291.   if (e3)
  2292.     arg_types = tree_cons (NULL_TREE, ptr_type_node, build_tree_list (NULL_TREE, sizetype));
  2293.   else if (e3 |= e2)
  2294.     {
  2295.       if (arg_types == NULL_TREE)
  2296.     arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
  2297.       else
  2298.     arg_types = tree_cons (NULL_TREE, ptr_type_node, TREE_CHAIN (arg_types));
  2299.     }
  2300.   else e3 |= e1;
  2301.  
  2302.   if (e3)
  2303.     type = build_function_type (void_type_node, arg_types);
  2304.  
  2305.   return type;
  2306. }
  2307.  
  2308. static void
  2309. mark_vtable_entries (decl)
  2310.      tree decl;
  2311. {
  2312.   tree entries = TREE_CHAIN (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)));
  2313.  
  2314.   if (flag_dossier)
  2315.     entries = TREE_CHAIN (entries);
  2316.  
  2317.   for (; entries; entries = TREE_CHAIN (entries))
  2318.     {
  2319.       tree fnaddr = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (entries));
  2320.       tree fn = TREE_OPERAND (fnaddr, 0);
  2321.       TREE_ADDRESSABLE (fn) = 1;
  2322.       if (DECL_ABSTRACT_VIRTUAL_P (fn))
  2323.     {
  2324.       extern tree abort_fndecl;
  2325.       if (flag_vtable_thunks)
  2326.         fnaddr = TREE_VALUE (entries);
  2327.       TREE_OPERAND (fnaddr, 0) = abort_fndecl;
  2328.     }
  2329.     }
  2330. }
  2331.  
  2332. /* Set TREE_PUBLIC and/or TREE_EXTERN on the vtable DECL,
  2333.    based on TYPE and other static flags.
  2334.  
  2335.    Note that anything public is tagged TREE_PUBLIC, whether
  2336.    it's public in this file or in another one.  */
  2337.  
  2338. void
  2339. import_export_vtable (decl, type)
  2340.   tree decl, type;
  2341. {
  2342.   if (write_virtuals >= 2
  2343.       || CLASSTYPE_TEMPLATE_INSTANTIATION (type))
  2344.     {
  2345.       if (CLASSTYPE_INTERFACE_KNOWN (type))
  2346.     {
  2347.       TREE_PUBLIC (decl) = 1;
  2348.       DECL_EXTERNAL (decl) = ! CLASSTYPE_VTABLE_NEEDS_WRITING (type);
  2349.     }
  2350.     }
  2351.   else if (write_virtuals != 0)
  2352.     {
  2353.       TREE_PUBLIC (decl) = 1;
  2354.       if (write_virtuals < 0)
  2355.     DECL_EXTERNAL (decl) = 1;
  2356.     }
  2357. }
  2358.  
  2359. static void
  2360. import_export_template (type)
  2361.      tree type;
  2362. {
  2363.   if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
  2364.       && ! flag_implicit_templates
  2365.       && CLASSTYPE_INTERFACE_UNKNOWN (type))
  2366.     {
  2367.       SET_CLASSTYPE_INTERFACE_KNOWN (type);
  2368.       CLASSTYPE_INTERFACE_ONLY (type) = 1;
  2369.       CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 0;
  2370.     }
  2371. }
  2372.     
  2373. static void
  2374. finish_vtable_vardecl (prev, vars)
  2375.      tree prev, vars;
  2376. {
  2377.   tree ctype = DECL_CONTEXT (vars);
  2378.   import_export_template (ctype);
  2379.   import_export_vtable (vars, ctype);
  2380.  
  2381.   if (flag_vtable_thunks && !CLASSTYPE_INTERFACE_KNOWN (ctype))
  2382.     {
  2383.       tree method;
  2384.       for (method = CLASSTYPE_METHODS (ctype); method != NULL_TREE;
  2385.        method = DECL_NEXT_METHOD (method))
  2386.     {
  2387.       if (DECL_VINDEX (method) != NULL_TREE && !DECL_SAVED_INSNS (method)
  2388.           && !DECL_ABSTRACT_VIRTUAL_P (method))
  2389.         {
  2390.           SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
  2391.           CLASSTYPE_INTERFACE_ONLY (ctype) = DECL_EXTERNAL (method);
  2392.           TREE_PUBLIC (vars) = 1;
  2393.           DECL_EXTERNAL (vars) = DECL_EXTERNAL (method);
  2394.           break;
  2395.         }
  2396.     }
  2397.     }
  2398.  
  2399.   if (write_virtuals >= 0
  2400.       && ! DECL_EXTERNAL (vars) && (TREE_PUBLIC (vars) || TREE_USED (vars)))
  2401.     {
  2402.       extern tree the_null_vtable_entry;
  2403.  
  2404.       /* Stuff this virtual function table's size into
  2405.      `pfn' slot of `the_null_vtable_entry'.  */
  2406.       tree nelts = array_type_nelts (TREE_TYPE (vars));
  2407.       if (flag_vtable_thunks)
  2408.     TREE_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (vars))) = nelts;
  2409.       else
  2410.     SET_FNADDR_FROM_VTABLE_ENTRY (the_null_vtable_entry, nelts);
  2411.       /* Kick out the dossier before writing out the vtable.  */
  2412.       if (flag_dossier)
  2413.     rest_of_decl_compilation (TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (TREE_CHAIN (CONSTRUCTOR_ELTS (DECL_INITIAL (vars))))), 0), 0, 1, 1);
  2414.  
  2415.       /* Write it out.  */
  2416.       mark_vtable_entries (vars);
  2417.       if (TREE_TYPE (DECL_INITIAL (vars)) == 0)
  2418.       store_init_value (vars, DECL_INITIAL (vars));
  2419.  
  2420. #ifdef DWARF_DEBUGGING_INFO
  2421.       if (write_symbols == DWARF_DEBUG)
  2422.     {
  2423.       /* Mark the VAR_DECL node representing the vtable itself as a
  2424.          "gratuitous" one, thereby forcing dwarfout.c to ignore it.
  2425.          It is rather important that such things be ignored because
  2426.          any effort to actually generate DWARF for them will run
  2427.          into trouble when/if we encounter code like:
  2428.  
  2429.         #pragma interface
  2430.         struct S { virtual void member (); };
  2431.  
  2432.           because the artificial declaration of the vtable itself (as
  2433.           manufactured by the g++ front end) will say that the vtable
  2434.           is a static member of `S' but only *after* the debug output
  2435.           for the definition of `S' has already been output.  This causes
  2436.           grief because the DWARF entry for the definition of the vtable
  2437.           will try to refer back to an earlier *declaration* of the
  2438.           vtable as a static member of `S' and there won't be one.
  2439.           We might be able to arrange to have the "vtable static member"
  2440.           attached to the member list for `S' before the debug info for
  2441.           `S' get written (which would solve the problem) but that would
  2442.           require more intrusive changes to the g++ front end.  */
  2443.  
  2444.       DECL_IGNORED_P (vars) = 1;
  2445.     }
  2446. #endif /* DWARF_DEBUGGING_INFO */
  2447.  
  2448.       rest_of_decl_compilation (vars, NULL_PTR, 1, 1);
  2449.     }
  2450.   else if (TREE_USED (vars) && flag_vtable_thunks)
  2451.     assemble_external (vars);
  2452.   /* We know that PREV must be non-zero here.  */
  2453.   TREE_CHAIN (prev) = TREE_CHAIN (vars);
  2454. }
  2455.  
  2456. void
  2457. walk_vtables (typedecl_fn, vardecl_fn)
  2458.      register void (*typedecl_fn)();
  2459.      register void (*vardecl_fn)();
  2460. {
  2461.   tree prev, vars;
  2462.  
  2463.   for (prev = 0, vars = getdecls (); vars; vars = TREE_CHAIN (vars))
  2464.     {
  2465.       register tree type = TREE_TYPE (vars);
  2466.  
  2467.       if (TREE_CODE (vars) == TYPE_DECL
  2468.       && type != error_mark_node
  2469.       && TYPE_LANG_SPECIFIC (type)
  2470.       && CLASSTYPE_VSIZE (type))
  2471.     {
  2472.       if (typedecl_fn) (*typedecl_fn) (prev, vars);
  2473.     }
  2474.       else if (TREE_CODE (vars) == VAR_DECL && DECL_VIRTUAL_P (vars))
  2475.     {
  2476.       if (vardecl_fn) (*vardecl_fn) (prev, vars);
  2477.     }
  2478.       else
  2479.     prev = vars;
  2480.     }
  2481. }
  2482.  
  2483. static void
  2484. finish_sigtable_vardecl (prev, vars)
  2485.      tree prev, vars;
  2486. {
  2487.   /* We don't need to mark sigtable entries as addressable here as is done
  2488.      for vtables.  Since sigtables, unlike vtables, are always written out,
  2489.      that was already done in build_signature_table_constructor.  */
  2490.  
  2491.   rest_of_decl_compilation (vars, NULL_PTR, 1, 1);
  2492.  
  2493.   /* We know that PREV must be non-zero here.  */
  2494.   TREE_CHAIN (prev) = TREE_CHAIN (vars);
  2495. }
  2496.  
  2497. void
  2498. walk_sigtables (typedecl_fn, vardecl_fn)
  2499.      register void (*typedecl_fn)();
  2500.      register void (*vardecl_fn)();
  2501. {
  2502.   tree prev, vars;
  2503.  
  2504.   for (prev = 0, vars = getdecls (); vars; vars = TREE_CHAIN (vars))
  2505.     {
  2506.       register tree type = TREE_TYPE (vars);
  2507.  
  2508.       if (TREE_CODE (vars) == TYPE_DECL
  2509.       && type != error_mark_node
  2510.       && IS_SIGNATURE (type))
  2511.     {
  2512.       if (typedecl_fn) (*typedecl_fn) (prev, vars);
  2513.     }
  2514.       else if (TREE_CODE (vars) == VAR_DECL
  2515.            && TREE_TYPE (vars) != error_mark_node
  2516.            && IS_SIGNATURE (TREE_TYPE (vars)))
  2517.     {
  2518.       if (vardecl_fn) (*vardecl_fn) (prev, vars);
  2519.     }
  2520.       else
  2521.     prev = vars;
  2522.     }
  2523. }
  2524.  
  2525. extern int parse_time, varconst_time;
  2526.  
  2527. #define TIMEVAR(VAR, BODY)    \
  2528. do { int otime = get_run_time (); BODY; VAR += get_run_time () - otime; } while (0)
  2529.  
  2530. /* This routine is called from the last rule in yyparse ().
  2531.    Its job is to create all the code needed to initialize and
  2532.    destroy the global aggregates.  We do the destruction
  2533.    first, since that way we only need to reverse the decls once.  */
  2534.  
  2535. void
  2536. finish_file ()
  2537. {
  2538.   extern int lineno;
  2539.   int start_time, this_time;
  2540.  
  2541.   tree fnname;
  2542.   tree vars = static_aggregates;
  2543.   int needs_cleaning = 0, needs_messing_up = 0;
  2544.  
  2545.   build_exception_table ();
  2546.  
  2547.   if (flag_detailed_statistics)
  2548.     dump_tree_statistics ();
  2549.  
  2550.   /* Bad parse errors.  Just forget about it.  */
  2551.   if (! global_bindings_p () || current_class_type)
  2552.     return;
  2553.  
  2554.   start_time = get_run_time ();
  2555.  
  2556.   /* Push into C language context, because that's all
  2557.      we'll need here.  */
  2558.   push_lang_context (lang_name_c);
  2559.  
  2560.   /* Set up the name of the file-level functions we may need.  */
  2561.   /* Use a global object (which is already required to be unique over
  2562.      the program) rather than the file name (which imposes extra
  2563.      constraints).  -- Raeburn@MIT.EDU, 10 Jan 1990.  */
  2564.  
  2565.   /* See if we really need the hassle.  */
  2566.   while (vars && needs_cleaning == 0)
  2567.     {
  2568.       tree decl = TREE_VALUE (vars);
  2569.       tree type = TREE_TYPE (decl);
  2570.       if (TYPE_NEEDS_DESTRUCTOR (type))
  2571.     {
  2572.       needs_cleaning = 1;
  2573.       needs_messing_up = 1;
  2574.       break;
  2575.     }
  2576.       else
  2577.     needs_messing_up |= TYPE_NEEDS_CONSTRUCTING (type);
  2578.       vars = TREE_CHAIN (vars);
  2579.     }
  2580.   if (needs_cleaning == 0)
  2581.     goto mess_up;
  2582.  
  2583.   /* Otherwise, GDB can get confused, because in only knows
  2584.      about source for LINENO-1 lines.  */
  2585.   lineno -= 1;
  2586.  
  2587.   fnname = get_file_function_name ('D');
  2588.   start_function (void_list_node, build_parse_node (CALL_EXPR, fnname, void_list_node, NULL_TREE), 0, 0);
  2589.   fnname = DECL_ASSEMBLER_NAME (current_function_decl);
  2590.   store_parm_decls ();
  2591.  
  2592.   pushlevel (0);
  2593.   clear_last_expr ();
  2594.   push_momentary ();
  2595.   expand_start_bindings (0);
  2596.  
  2597.   /* These must be done in backward order to destroy,
  2598.      in which they happen to be!  */
  2599.   while (vars)
  2600.     {
  2601.       tree decl = TREE_VALUE (vars);
  2602.       tree type = TREE_TYPE (decl);
  2603.       tree temp = TREE_PURPOSE (vars);
  2604.  
  2605.       if (TYPE_NEEDS_DESTRUCTOR (type))
  2606.     {
  2607.       if (TREE_STATIC (vars))
  2608.         expand_start_cond (build_binary_op (NE_EXPR, temp, integer_zero_node, 1), 0);
  2609.       if (TREE_CODE (type) == ARRAY_TYPE)
  2610.         temp = decl;
  2611.       else
  2612.         {
  2613.           mark_addressable (decl);
  2614.           temp = build1 (ADDR_EXPR, TYPE_POINTER_TO (type), decl);
  2615.         }
  2616.       temp = build_delete (TREE_TYPE (temp), temp,
  2617.                    integer_two_node, LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
  2618.       expand_expr_stmt (temp);
  2619.  
  2620.       if (TREE_STATIC (vars))
  2621.         expand_end_cond ();
  2622.     }
  2623.       vars = TREE_CHAIN (vars);
  2624.     }
  2625.  
  2626.   expand_end_bindings (getdecls(), 1, 0);
  2627.   poplevel (1, 0, 0);
  2628.   pop_momentary ();
  2629.  
  2630.   finish_function (lineno, 0);
  2631.  
  2632.   assemble_destructor (IDENTIFIER_POINTER (fnname));
  2633.  
  2634.   /* if it needed cleaning, then it will need messing up: drop through  */
  2635.  
  2636.  mess_up:
  2637.   /* Must do this while we think we are at the top level.  */
  2638.   vars = nreverse (static_aggregates);
  2639.   if (vars != NULL_TREE)
  2640.     {
  2641.       fnname = get_file_function_name ('I');
  2642.       start_function (void_list_node, build_parse_node (CALL_EXPR, fnname, void_list_node, NULL_TREE), 0, 0);
  2643.       fnname = DECL_ASSEMBLER_NAME (current_function_decl);
  2644.       store_parm_decls ();
  2645.  
  2646.       pushlevel (0);
  2647.       clear_last_expr ();
  2648.       push_momentary ();
  2649.       expand_start_bindings (0);
  2650.  
  2651.       while (vars)
  2652.     {
  2653.       tree decl = TREE_VALUE (vars);
  2654.       tree init = TREE_PURPOSE (vars);
  2655.       tree old_cleanups = cleanups_this_call;
  2656.  
  2657.       /* If this was a static attribute within some function's scope,
  2658.          then don't initialize it here.  Also, don't bother
  2659.          with initializers that contain errors.  */
  2660.       if (TREE_STATIC (vars)
  2661.           || (init && TREE_CODE (init) == TREE_LIST
  2662.           && value_member (error_mark_node, init)))
  2663.         {
  2664.           vars = TREE_CHAIN (vars);
  2665.           continue;
  2666.         }
  2667.  
  2668.       if (TREE_CODE (decl) == VAR_DECL)
  2669.         {
  2670.           /* Set these global variables so that GDB at least puts
  2671.          us near the declaration which required the initialization.  */
  2672.           input_filename = DECL_SOURCE_FILE (decl);
  2673.           lineno = DECL_SOURCE_LINE (decl);
  2674.           emit_note (input_filename, lineno);
  2675.  
  2676.           /* 9.5p5: The initializer of a static member of a class has
  2677.          the same acess rights as a member function.  */
  2678.           DECL_CLASS_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
  2679.  
  2680.           if (init)
  2681.         {
  2682.           if (TREE_CODE (init) == VAR_DECL)
  2683.             {
  2684.               /* This behavior results when there are
  2685.              multiple declarations of an aggregate,
  2686.              the last of which defines it.  */
  2687.               if (DECL_RTL (init) == DECL_RTL (decl))
  2688.             {
  2689.               my_friendly_assert (DECL_INITIAL (decl) == error_mark_node
  2690.                   || (TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR
  2691.                       && CONSTRUCTOR_ELTS (DECL_INITIAL (decl)) == NULL_TREE),
  2692.                           199);
  2693.               init = DECL_INITIAL (init);
  2694.               if (TREE_CODE (init) == CONSTRUCTOR
  2695.                   && CONSTRUCTOR_ELTS (init) == NULL_TREE)
  2696.                 init = NULL_TREE;
  2697.             }
  2698. #if 0
  2699.               else if (TREE_TYPE (decl) == TREE_TYPE (init))
  2700.             {
  2701. #if 1
  2702.               my_friendly_abort (200);
  2703. #else
  2704.               /* point to real decl's rtl anyway.  */
  2705.               DECL_RTL (init) = DECL_RTL (decl);
  2706.               my_friendly_assert (DECL_INITIAL (decl) == error_mark_node,
  2707.                           201);
  2708.               init = DECL_INITIAL (init);
  2709. #endif                /* 1 */
  2710.             }
  2711. #endif                /* 0 */
  2712.             }
  2713.         }
  2714.           if (IS_AGGR_TYPE (TREE_TYPE (decl))
  2715.           || init == 0
  2716.           || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
  2717.         expand_aggr_init (decl, init, 0);
  2718.           else if (TREE_CODE (init) == TREE_VEC)
  2719.         {
  2720.           expand_expr (expand_vec_init (decl, TREE_VEC_ELT (init, 0),
  2721.                         TREE_VEC_ELT (init, 1),
  2722.                         TREE_VEC_ELT (init, 2), 0),
  2723.                    const0_rtx, VOIDmode, 0);
  2724.           free_temp_slots ();
  2725.         }
  2726.           else
  2727.         expand_assignment (decl, init, 0, 0);
  2728.  
  2729.           DECL_CLASS_CONTEXT (current_function_decl) = NULL_TREE;
  2730.         }
  2731.       else if (TREE_CODE (decl) == SAVE_EXPR)
  2732.         {
  2733.           if (! PARM_DECL_EXPR (decl))
  2734.         {
  2735.           /* a `new' expression at top level.  */
  2736.           expand_expr (decl, const0_rtx, VOIDmode, 0);
  2737.           free_temp_slots ();
  2738.           expand_aggr_init (build_indirect_ref (decl, NULL_PTR), init, 0);
  2739.         }
  2740.         }
  2741.       else if (decl == error_mark_node)
  2742.         ;
  2743.       else my_friendly_abort (22);
  2744.       vars = TREE_CHAIN (vars);
  2745.       /* Cleanup any temporaries needed for the initial value.  */
  2746.       expand_cleanups_to (old_cleanups);
  2747.     }
  2748.  
  2749.       expand_end_bindings (getdecls(), 1, 0);
  2750.       poplevel (1, 0, 0);
  2751.       pop_momentary ();
  2752.  
  2753.       finish_function (lineno, 0);
  2754.       assemble_constructor (IDENTIFIER_POINTER (fnname));
  2755.     }
  2756.  
  2757.   /* Done with C language context needs.  */
  2758.   pop_lang_context ();
  2759.  
  2760.   /* Now write out any static class variables (which may have since
  2761.      learned how to be initialized).  */
  2762.   while (pending_statics)
  2763.     {
  2764.       tree decl = TREE_VALUE (pending_statics);
  2765.       if (TREE_USED (decl) == 1
  2766.       || TREE_READONLY (decl) == 0
  2767.       || DECL_INITIAL (decl) == 0)
  2768.     rest_of_decl_compilation (decl, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), 1, 1);
  2769.       pending_statics = TREE_CHAIN (pending_statics);
  2770.     }
  2771.  
  2772.   this_time = get_run_time ();
  2773.   parse_time -= this_time - start_time;
  2774.   varconst_time += this_time - start_time;
  2775.  
  2776.   start_time = get_run_time ();
  2777.  
  2778.   /* Now delete from the chain of variables all virtual function tables.
  2779.      We output them all ourselves, because each will be treated specially.  */
  2780.  
  2781. #if 1
  2782.   /* The reason for pushing garbage onto the global_binding_level is to
  2783.      ensure that we can slice out _DECLs which pertain to virtual function
  2784.      tables.  If the last thing pushed onto the global_binding_level was a
  2785.      virtual function table, then slicing it out would slice away all the
  2786.      decls (i.e., we lose the head of the chain).
  2787.  
  2788.      There are several ways of getting the same effect, from changing the
  2789.      way that iterators over the chain treat the elements that pertain to
  2790.      virtual function tables, moving the implementation of this code to
  2791.      decl.c (where we can manipulate global_binding_level directly),
  2792.      popping the garbage after pushing it and slicing away the vtable
  2793.      stuff, or just leaving it alone. */
  2794.  
  2795.   /* Make last thing in global scope not be a virtual function table.  */
  2796. #if 0 /* not yet, should get fixed properly later */
  2797.   vars = make_type_decl (get_identifier (" @%$#@!"), integer_type_node);
  2798. #else
  2799.   vars = build_decl (TYPE_DECL, get_identifier (" @%$#@!"), integer_type_node);
  2800. #endif
  2801.   DECL_IGNORED_P (vars) = 1;
  2802.   SET_DECL_ARTIFICIAL (vars);
  2803.   pushdecl (vars);
  2804. #endif
  2805.  
  2806.   walk_vtables ((void (*)())0, finish_vtable_vardecl);
  2807.   if (flag_handle_signatures)
  2808.     walk_sigtables ((void (*)())0, finish_sigtable_vardecl);
  2809.  
  2810.   for (vars = getdecls (); vars; vars = TREE_CHAIN (vars))
  2811.     {
  2812.       if (TREE_CODE (vars) == THUNK_DECL)
  2813.     emit_thunk (vars);
  2814.     }
  2815.  
  2816.   {
  2817.     int reconsider = 0;        /* More may be referenced; check again */
  2818.     tree delayed = NULL_TREE;    /* These might be referenced later */
  2819.  
  2820.     /* Now write out inline functions which had their addresses taken and
  2821.        which were not declared virtual and which were not declared `extern
  2822.        inline'.  */
  2823.     while (saved_inlines)
  2824.       {
  2825.     tree decl = TREE_VALUE (saved_inlines);
  2826.     saved_inlines = TREE_CHAIN (saved_inlines);
  2827.     /* Redefinition of a member function can cause DECL_SAVED_INSNS to be
  2828.        0; don't crash.  */
  2829.     if (TREE_ASM_WRITTEN (decl) || DECL_SAVED_INSNS (decl) == 0)
  2830.       continue;
  2831.     if (DECL_FUNCTION_MEMBER_P (decl) && !TREE_PUBLIC (decl))
  2832.       {
  2833.         tree ctype = DECL_CLASS_CONTEXT (decl);
  2834.         if (CLASSTYPE_INTERFACE_KNOWN (ctype))
  2835.           {
  2836.         TREE_PUBLIC (decl) = 1;
  2837.         DECL_EXTERNAL (decl)
  2838.           = (CLASSTYPE_INTERFACE_ONLY (ctype)
  2839.              || (DECL_INLINE (decl) && ! flag_implement_inlines));
  2840.           }
  2841.       }
  2842.     if (TREE_PUBLIC (decl)
  2843.         || TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
  2844.         || flag_keep_inline_functions)
  2845.       {
  2846.         if (DECL_EXTERNAL (decl)
  2847.         || (DECL_IMPLICIT_INSTANTIATION (decl)
  2848.             && ! flag_implicit_templates))
  2849.           assemble_external (decl);
  2850.         else
  2851.           {
  2852.         reconsider = 1;
  2853.         temporary_allocation ();
  2854.         output_inline_function (decl);
  2855.         permanent_allocation (1);
  2856.           }
  2857.       }
  2858.     else if (TREE_USED (decl)
  2859.          || TREE_USED (DECL_ASSEMBLER_NAME (decl)))
  2860.       delayed = tree_cons (NULL_TREE, decl, delayed);
  2861.       }
  2862.  
  2863.     if (reconsider && delayed)
  2864.       {
  2865.     while (reconsider)
  2866.       {
  2867.         tree place;
  2868.         reconsider = 0;
  2869.         for (place = delayed; place; place = TREE_CHAIN (place))
  2870.           {
  2871.         tree decl = TREE_VALUE (place);
  2872.         if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
  2873.             && ! TREE_ASM_WRITTEN (decl))
  2874.           {
  2875.             if (DECL_EXTERNAL (decl)
  2876.             || (DECL_IMPLICIT_INSTANTIATION (decl)
  2877.                 && ! flag_implicit_templates))
  2878.               assemble_external (decl);
  2879.             else
  2880.               {
  2881.             reconsider = 1;
  2882.             temporary_allocation ();
  2883.             output_inline_function (decl);
  2884.             permanent_allocation (1);
  2885.               }
  2886.           }
  2887.           }
  2888.       }
  2889.       }
  2890.   }
  2891.  
  2892.   if (write_virtuals == 2)
  2893.     {
  2894.       /* Now complain about an virtual function tables promised
  2895.      but not delivered.  */
  2896.       while (pending_vtables)
  2897.     {
  2898.       if (TREE_PURPOSE (pending_vtables) == NULL_TREE)
  2899.         error ("virtual function table for `%s' not defined",
  2900.            IDENTIFIER_POINTER (TREE_VALUE (pending_vtables)));
  2901.       pending_vtables = TREE_CHAIN (pending_vtables);
  2902.     }
  2903.     }
  2904.  
  2905.   permanent_allocation (1);
  2906.   this_time = get_run_time ();
  2907.   parse_time -= this_time - start_time;
  2908.   varconst_time += this_time - start_time;
  2909.  
  2910.   if (flag_detailed_statistics)
  2911.     dump_time_statistics ();
  2912. }
  2913.  
  2914. /* This is something of the form 'A()()()()()+1' that has turned out to be an
  2915.    expr.  Since it was parsed like a type, we need to wade through and fix
  2916.    that.  Unfortunately, since operator() is left-associative, we can't use
  2917.    tail recursion.  In the above example, TYPE is `A', and DECL is
  2918.    `()()()()()'.
  2919.  
  2920.    Maybe this shouldn't be recursive, but how often will it actually be
  2921.    used?  (jason) */
  2922. tree
  2923. reparse_absdcl_as_expr (type, decl)
  2924.      tree type, decl;
  2925. {
  2926.   /* do build_functional_cast (type, NULL_TREE) at bottom */
  2927.   if (TREE_OPERAND (decl, 0) == NULL_TREE)
  2928.     return build_functional_cast (type, NULL_TREE);
  2929.  
  2930.   /* recurse */
  2931.   decl = reparse_decl_as_expr (type, TREE_OPERAND (decl, 0));
  2932.  
  2933.   decl = build_x_function_call (decl, NULL_TREE, current_class_decl);
  2934.  
  2935.   if (TREE_CODE (decl) == CALL_EXPR && TREE_TYPE (decl) != void_type_node)
  2936.     decl = require_complete_type (decl);
  2937.  
  2938.   return decl;
  2939. }
  2940.  
  2941. /* This is something of the form `int ((int)(int)(int)1)' that has turned
  2942.    out to be an expr.  Since it was parsed like a type, we need to wade
  2943.    through and fix that.  Since casts are right-associative, we are
  2944.    reversing the order, so we don't have to recurse.
  2945.  
  2946.    In the above example, DECL is the `(int)(int)(int)', and EXPR is the
  2947.    `1'.  */
  2948. tree
  2949. reparse_absdcl_as_casts (decl, expr)
  2950.      tree decl, expr;
  2951. {
  2952.   tree type;
  2953.   
  2954.   if (TREE_CODE (expr) == CONSTRUCTOR)
  2955.     {
  2956.       type = groktypename (TREE_VALUE (TREE_OPERAND (decl, 1)));
  2957.       decl = TREE_OPERAND (decl, 0);
  2958.  
  2959.       if (IS_SIGNATURE (type))
  2960.     {
  2961.       error ("cast specifies signature type");
  2962.       return error_mark_node;
  2963.     }
  2964.  
  2965.       expr = digest_init (type, expr, (tree *) 0);
  2966.       if (TREE_CODE (type) == ARRAY_TYPE && TYPE_SIZE (type) == 0)
  2967.     {
  2968.       int failure = complete_array_type (type, expr, 1);
  2969.       if (failure)
  2970.         my_friendly_abort (78);
  2971.     }
  2972.     }
  2973.  
  2974.   while (decl)
  2975.     {
  2976.       type = groktypename (TREE_VALUE (TREE_OPERAND (decl, 1)));
  2977.       decl = TREE_OPERAND (decl, 0);
  2978.       expr = build_c_cast (type, expr);
  2979.     }
  2980.  
  2981.   return expr;
  2982. }
  2983.  
  2984. /* Recursive helper function for reparse_decl_as_expr.  It may be a good
  2985.    idea to reimplement this using an explicit stack, rather than recursion. */
  2986. static tree
  2987. reparse_decl_as_expr1 (decl)
  2988.      tree decl;
  2989. {
  2990.   switch (TREE_CODE (decl))
  2991.     {
  2992.     case IDENTIFIER_NODE:
  2993.       return do_identifier (decl);
  2994.     case INDIRECT_REF:
  2995.       return build_x_indirect_ref
  2996.     (reparse_decl_as_expr1 (TREE_OPERAND (decl, 0)), "unary *");
  2997.     case ADDR_EXPR:
  2998.       return build_x_unary_op (ADDR_EXPR,
  2999.                    reparse_decl_as_expr1 (TREE_OPERAND (decl, 0)));
  3000.     case BIT_NOT_EXPR:
  3001.       return build_x_unary_op (BIT_NOT_EXPR,
  3002.                    reparse_decl_as_expr1 (TREE_OPERAND (decl, 0)));
  3003.     case SCOPE_REF:
  3004.       return build_offset_ref (TREE_OPERAND (decl, 0), TREE_OPERAND (decl, 1));
  3005.     case ARRAY_REF:
  3006.       return grok_array_decl (reparse_decl_as_expr1 (TREE_OPERAND (decl, 0)),
  3007.                   TREE_OPERAND (decl, 1));
  3008.     default:
  3009.       my_friendly_abort (5);
  3010.       return NULL_TREE;
  3011.     }
  3012. }
  3013.  
  3014. /* This is something of the form `int (*a)++' that has turned out to be an
  3015.    expr.  It was only converted into parse nodes, so we need to go through
  3016.    and build up the semantics.  Most of the work is done by
  3017.    reparse_decl_as_expr1, above.
  3018.  
  3019.    In the above example, TYPE is `int' and DECL is `*a'.  */
  3020. tree
  3021. reparse_decl_as_expr (type, decl)
  3022.      tree type, decl;
  3023. {
  3024.   decl = build_tree_list (NULL_TREE, reparse_decl_as_expr1 (decl));
  3025.   return build_functional_cast (type, decl);
  3026. }
  3027.  
  3028. /* This is something of the form `int (*a)' that has turned out to be a
  3029.    decl.  It was only converted into parse nodes, so we need to do the
  3030.    checking that make_{pointer,reference}_declarator do. */
  3031.  
  3032. tree
  3033. finish_decl_parsing (decl)
  3034.      tree decl;
  3035. {
  3036.   extern int current_class_depth;
  3037.   
  3038.   switch (TREE_CODE (decl))
  3039.     {
  3040.     case IDENTIFIER_NODE:
  3041.       return decl;
  3042.     case INDIRECT_REF:
  3043.       return make_pointer_declarator
  3044.     (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
  3045.     case ADDR_EXPR:
  3046.       return make_reference_declarator
  3047.     (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
  3048.     case BIT_NOT_EXPR:
  3049.       TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
  3050.       return decl;
  3051.     case SCOPE_REF:
  3052.       push_nested_class (TREE_TYPE (TREE_OPERAND (decl, 0)), 3);
  3053.       TREE_COMPLEXITY (decl) = current_class_depth;
  3054.       return decl;
  3055.     case ARRAY_REF:
  3056.       TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
  3057.       return decl;
  3058.     default:
  3059.       my_friendly_abort (5);
  3060.       return NULL_TREE;
  3061.     }
  3062. }
  3063.  
  3064. tree
  3065. check_cp_case_value (value)
  3066.      tree value;
  3067. {
  3068.   if (value == NULL_TREE)
  3069.     return value;
  3070.  
  3071.   /* build_c_cast puts on a NOP_EXPR to make a non-lvalue.
  3072.      Strip such NOP_EXPRs.  */
  3073.   if (TREE_CODE (value) == NOP_EXPR
  3074.       && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
  3075.     value = TREE_OPERAND (value, 0);
  3076.  
  3077.   if (TREE_READONLY_DECL_P (value))
  3078.     {
  3079.       value = decl_constant_value (value);
  3080.       /* build_c_cast puts on a NOP_EXPR to make a non-lvalue.
  3081.      Strip such NOP_EXPRs.  */
  3082.       if (TREE_CODE (value) == NOP_EXPR
  3083.       && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
  3084.     value = TREE_OPERAND (value, 0);
  3085.     }
  3086.   value = fold (value);
  3087.  
  3088.   if (TREE_CODE (value) != INTEGER_CST
  3089.       && value != error_mark_node)
  3090.     {
  3091.       cp_error ("case label `%E' does not reduce to an integer constant",
  3092.         value);
  3093.       value = error_mark_node;
  3094.     }
  3095.   else
  3096.     /* Promote char or short to int.  */
  3097.     value = default_conversion (value);
  3098.  
  3099.   constant_expression_warning (value);
  3100.  
  3101.   return value;
  3102. }
  3103.